How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask chrisaw Your Own Question

chrisaw
chrisaw, DevOps Manager
Category: Software
Satisfied Customers: 13
Experience:  DevOps Manager with over 10 years experience in the DevOps and IT field.
58355812
Type Your Software Question Here...
chrisaw is online now

PHP Help. Would anyone be able to help provide the code that

Customer Question

PHP Help. Would anyone be able to help provide the code that will allow to get started for this assignment? I would really appreciate it.
After user signs in successfully with login.html and login.php, redirect user to start.php or allow user to go to start.php. In start.php, randomly select one php from sql1.php-sql10.php (with random functions) and add it as a link in start.php (an example shown in Fig.3B-1) so that user can navigate to the selected page; For example, if sql3.php is selected, then the user will be able to click and visit sql3.php from start.php after signing in. Fig. 3B-1 Current Page(start.php) Next Page(sql3.php) 3. When a user click on Next Page, a new page will be randomly selected from sql1.phpsql10.php, excluding those have already been selected. For example, if the user click on sql3.php (shown in Fig. 3B-1), the page of sql3.php will display new information (shown in Fig. 3B-2) with the new selected page (assume it is sql7.php this time and sql3.php shouldn’t be selected). Fig. 3B-2 4. Allow user to click on either Prev page to return to previous page or next page every time when the user lands on a new page. 5. If all the sql1.php-sql10.php files are randomly selected in the above process, then in the last selected page, don’t display the link to Next Page.
Submitted: 8 months ago.
Category: Software
Customer: replied 8 months ago.
( MUST INCLUDE STACKS AND SESSIONS!!)
Customer: replied 8 months ago.
I have added the specificition. Question i want answered starts at 3B. Thank you
Expert:  chrisaw replied 8 months ago.
Hi there! Just to confirm - you have already written the login part of the code or is that something you could use a hand with too? As for the start.php part: <?php$next_page = rand(1,10);echo 'Current Page (start.php)&nbsp;&nbsp;|&nbsp;&nbsp;';echo '<a href="sql'.$next_page.'">Next Page (sql'.$next_page.'.php)</a>';?> In order to navigate back from each of the sql*.php files you can use:<?phpif(isset($_SERVER['HTTP_REFERER'])){ // Determine previous page from referrer header if (strpos($_SERVER['HTTP_REFERER'], '/') !== false) { $previous_url = explode('/', $_SERVER['HTTP_REFERER']); $previous_page = end($previous_url); } else { $previous_page = $_SERVER['HTTP_REFERER']; } echo '<a href="'.$previous_page.'</a>Prev page ('.$previous_page.')</a>';}else{ // Fallback if referrer header not detected echo '<a href="javascript:history.go(-1)">Prev Page</a>';} That will use HTTP headers if possible (which will allow you to show the previous page name) but will fallback to a javascript function (which can't display the page name) if the header is not available. Hope this helps!
Expert:  chrisaw replied 8 months ago.
If the formatting was broken on that - I've added this to a pastebin link: http://pastie.org/private/rgpe2o88ixwbfrr6z9b4g
Customer: replied 8 months ago.
Yes the login part of the code and been complete and 3B onwards is what I have been troubling me. Does this code include the use stacks and sessions as I asked? I just ask because I know very little when it comes to PHP code but thank you for the response and I hope I can come to you again for assistance.
Expert:  chrisaw replied 8 months ago.
Hello again, I'm not really sure what you mean by "stacks" - I assume (based on reading through the project spec you attached) that this is simply referring to the navigation links which it would, indeed, have. As for sessions - a session is essentially a cookie which is stored both client and server side to recognise that a user is already logged in. In order to ensure the user is authenticated I would need your existing login code which you have written already. If you can provide that I'm more than happy to add that to the other pages. As for coming to me for assistance - any time!
Customer: replied 8 months ago.
By stacks I mean by PUSH, POP and TOP. There our essential for the project. Would this code be using these?
Customer: replied 8 months ago.
Could I also take you up on your offer to authenticated the existing login code which is written already? It would help and my work partner so much. Thank you.
Expert:  chrisaw replied 8 months ago.
Ahh I see - no this code doesn't use push, pop and top. Ah I see - what you essentially need is an array of numbers between 1 and 10 and then for them to be randomised like follows: $page_numbers = range(1, 10);shuffle($page_numbers); This will create you an array of numbers between 1 - 10 which you can then use pop to remove one they have been displayed once. I think I get what you're trying to achieve now. If you could send me the authentication code you have already written (feel free to just paste it or attach the file(s) here.) Once I have this - I will go through the full codebase and create & test the code to ensure it worked.
Customer: replied 8 months ago.
I see. Well here is the log in files and such and I would really appreciate it if you could maybe use stacks and sessions in the code. I hate asking but I really am stuck and I really appreciate the time and effort you have put in and can't thank you enough.
Customer: replied 8 months ago.
I hope i'm not coming across rude for asking this but again thank you.
Expert:  chrisaw replied 8 months ago.
Hi again, I’m just at work at the minute. I’ll have another look at this when I get home around 6pm UK time and should have a complete answer shortly after.
Customer: replied 8 months ago.
Thank you so very much. Even if you just look at the first question I asked with with the 'Start.PHP' and see how stacks could be fitted in that.
Expert:  chrisaw replied 8 months ago.
The login script you sent over looks absolutely fine - just working on the start.php and sql*.php stuff now and then I'll send that over shortly! Give me 15mins or so.
Expert:  chrisaw replied 8 months ago.
Hi again, The script is now complete and attached - I have also integrated it with your existing login and session handling code. Essentially it now keeps a json-encoded array stored in a cookie called 'remaining_pages' which has a list of pages not yet shown. Once this array is empty (i.e. all pages have been shown) the link then changes to a "Back to start" link which takes you back to start.php which then resets the cookie. You should just be able to start using this code right away. Any further questions just let me know!
Customer: replied 8 months ago.
Just to clarify a few things, does this you the use of stacks and session and be ready to be ready to hand up when needed? If so I can't thank you enough for your help and time. I am having a really tough time in my finale weeks in Uni and this will defiantly help me get a better understanding of stacks, sessions and PHP in general.
Expert:  chrisaw replied 8 months ago.
Hello again, Yes it includes both session tracking to ensure the user is already logged in and it uses stacks to keep track of what pages you have not yet visited and stores that in a cookie. No problem - I've been there myself. Best of luck with your uni course and just let me know if I can be of any further assistance. :) Since you're my first customer ever (and hopefully a happy one!) I'd greatly appreciate it if you could drop me a quick rating! Thanks a lot! Christopher
chrisaw, DevOps Manager
Category: Software
Satisfied Customers: 13
Experience: DevOps Manager with over 10 years experience in the DevOps and IT field.
chrisaw and 5 other Software Specialists are ready to help you
Customer: replied 8 months ago.
That's fantastic!! Believe me if I could I would give you six stars haha! I would recommend you in a heart beat. Thanks again :)
Customer: replied 8 months ago.
Sorry for bothering you again but I have another problem.
it's still not right a bit, it was supposed to be able to go back in the same order that you've went forward, which i don't think this does, it also gets stuck in a loop after a while and it needs a current page as well.Would you be able to fix that. Again sorry for the hassle
Expert:  chrisaw replied 8 months ago.
No hassle at all - I'll have a look at that now and get back to you very shortly.
Customer: replied 8 months ago.
Thank you. Here's the spec again if you need it.
Expert:  chrisaw replied 8 months ago.
Just keeping you in the loop - I'm still working on this. I can certainly see how this would give you a bit of a stressful time! I'll update you once I have a solution - should be within the next 30mins or so.
Customer: replied 8 months ago.
I've said many times but i can't thank you enough. its just it was supposed to be able to go back in the same order that you've went forward and current page as well.
Expert:  chrisaw replied 8 months ago.
No worries - I've fixed the current page issue already. I've actually got code in place to navigate backwards. Where it gets tricky is in allowing you to navigate backwards *but* not showing a "Next" link once you've seen all pages. That gets a bit tricky but still possible. I know how to do it - just trying to keep the code as simple and clean as possible since it's no use handing you reams and reams of hacky PHP to hand in. :)
Customer: replied 8 months ago.
Well hyper link buttons are needed to help navigate through the pages. Sorry if you didn't know that.
Customer: replied 8 months ago.
But i'm sure your doing a great job
Expert:  chrisaw replied 8 months ago.
Hello again, I have managed to get this one figured out in an elegant way. This should keep things simple but functional and still has your stack and session support built in. Let me know what you think and if there are any further problems just give me a nudge. Thanks again for your patience on this one! Best of luck with your course! Thanks for the tip by the way - greatly appreciated! :) Christohper
Customer: replied 8 months ago.
Hey there. Unfortunately I'm having trouble checking php on my own laptop but I just want to check if it can go back in the same order that you've went forward, make sure it didn't get stuck in a loop had a current page and had hypberlink buttons to help you navigate the page like the examples in gave in the spec.
Expert:  chrisaw replied 8 months ago.
Hi again, Yep - so the changes I've made now make navigation backwards work properly, current page is always displayed for all pages and next page navigates forward properly. The "next page" button is hidden when you are on the last page and the "prev page" button goes back to "start.php" when you are on the first page. I have tested the looping back and forth a few times and it does go in the correct order each time now. Please let me know if you have any further concerns and thanks for using JustAnswer and the 5 star review! Greatly appreciated. :)
Customer: replied 8 months ago.
Thank you. Is there any other way I can check how they pages display on my laptop? I seem to be having trouble. I understand it's late now so if I can we can talk again tomorrow as I feel where very close getting done :)
Expert:  chrisaw replied 8 months ago.
Sure! You can run PHP in "server mode" but in order for that to work you will need to disable the session part at the start of each file by commenting the first two lines out (with //) like the following: <?php// Ensure session is active//session_start();//include("check.php"); Then open command prompt and navigate to the correct directory by using the CD command like follows:cd "C:\Users\(username)\(path to php files)" Then start PHP in server mode with:php -S localhost:8000 Once you have done this - open a browser and navigate to: http://localhost:8000/start.php Hopefully this helps you testing. Remember to re-enable the session lines back to the following in start.php and sql[1-10].php before uploading these to the server / where ever they're ending up: <?php// Ensure session is activesession_start();include("check.php"); Thanks!
Customer: replied 8 months ago.
Thank you :) It's late now so i'll let you be but I can hope I come to you again for a few more questions to make sure it's all ready. Thanks again
Expert:  chrisaw replied 8 months ago.
Sure thing. Just drop me a response if you have any further questions. :)
Customer: replied 8 months ago.
Hey I'm pretty sure the code is done but I just want to clear a few things up before I leave you be haha! First could maybe explain to me how it all works, code and all? So I can leave this with a better understanding on how it works?
Expert:  chrisaw replied 8 months ago.
Sure, that's absolutely fine! As a high level overview:The way this works is by creating an array of pages (sql1-sql10), shuffles them to randomise the order, adds 'start.php' as the first page *after* they have been shuffled (this ensures that you always end up back at the start page.) It then stores this array in a cookie (in JSON format) and stores the page ID in a cookie also. The page_id value is then increased or decreased based on if you navigate forwards or backwards. All of this information is then used to generate the navigation block at the bottom of each page. If you are on the *first* page (sql1.php) the prev page link will take you back to start.php. If you are on the *last* page the next page link will not display as your project spec requires. On start.php: The following two lines are from your login script and are needed to ensure a session is currently active:session_start();include("check.php"); The following line creates an array with all of the pages (sql1 to sql10):$page_list = ['sql1.php','sql2.php','sql3.php','sql4.php','sql5.php','sql6.php','sql7.php','sql8.php','sql9.php','sql10.php']; The following line shuffles the array to randomise the order the pages are presented in:shuffle($page_list); The following line then adds 'start.php' to the start of this array (this ensures that the *first* page is always start.php (i.e. "Prev Page" on the first page in the queue will then navigate you back to the start page.):array_unshift($page_list, 'start.php'); The following line creates a cookie called 'page_list' and stores the above array in JSON format. This is to ensure that the array is a single line of text since an array cannot be directly stored in a cookie:setcookie('page_list', json_encode($page_list), time()+3600); The following line then creates another cookie called 'page_id' with 1 as the value - this is to define that the next page you see will be page 1 in the array above. The following two lines then print out the navigation links as you requested:echo 'Current Page ('.basename(__FILE__).')&nbsp;&nbsp;|&nbsp;&nbsp;';echo "<a href='$page_list[1]?page_id=1'>Next Page ($page_list[1])</a>"; On sql[1-10].php: The following two lines are from your login script and are needed to ensure a session is currently active:session_start();include("check.php"); The following line loads both the page_id value and the page_list array we defined on start.php:$page_list = json_decode($_COOKIE['page_list'], true);$page_id = $_GET['page_id']; Update the page_id cookie to the new value (if you have navigated *forward* the value will be +1, if you have navigated *back* it will be -1):setcookie('page_id', $_GET['page_id']); This will determine the previous page ID and name:$prev_id = $page_id-1;$prev_page = $page_list[$prev_id]; The following code will determine the ID for the next page *only* if this isn't the last page (i.e. sql10.php):if($page_id != 10){ $next_id = $page_id+1; $next_page = $page_list[$next_id];} The following line will write out the previous page link:echo "<a href='$prev_page?page_id=$prev_id'>Prev Page ($prev_page)</a>&nbsp;&nbsp;|&nbsp;&nbsp;"; The following line will determine the name of the current script and write out the current page text:echo 'Current Page ('.basename(__FILE__).')'; The following 4 lines will print out the next page link determined above *if* it was set above (i.e. if this isn't sql10.php):if(isset($next_page)){ echo "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='$next_page?page_id=$next_id'>Next Page ($next_page)</a>";} If you need anything else clarifying just let me know and I'll be more than happy to explain further if you can tell me which parts you need a hand with. :) Thanks! Christopher
Customer: replied 8 months ago.
Thank you :) Just another quick question. I have to write a page report which explains what technologies are used in
the implementation. What would that include exactly?
Expert:  chrisaw replied 8 months ago.
Sure - JSON, Cookies, PHP Arrays, GET variables and I think that’s about it!You should be able to find some blurb on each of these technologies on either Wikipedia or php.net and reword them a bit. ;)
Customer: replied 8 months ago.
Sorry to bother you again but I was looking through the code and I was curious where about's stack was used. It's just it's probably the most vital part of the code. It could be there and i'm not looking hard enough. That tends to happen a lot haha!
Expert:  chrisaw replied 8 months ago.
Hi again, Sure - so from your description of what was meant by "stack" it sounds like the idea was to use array functions. Examples of where these are used in the code are:- $page_list = ['sql1.php','sql2.php','sql3.php','sql4.php','sql5.php','sql6.php','sql7.php','sql8.php','sql9.php','sql10.php'];- shuffle($page_list);- array_unshift($page_list, 'start.php');- $page_list = json_decode($_COOKIE['page_list'], true);- $prev_page = $page_list[$prev_id];- $next_page = $page_list[$next_id]; Is that what you're looking for? Christopher
Customer: replied 8 months ago.
I think so. I'm really looking for Stack, abstract data type ypu know? Going back to the push and pop I mentioned before
Expert:  chrisaw replied 8 months ago.
Ah understood! Let me see if I can refactor the code a little to make use of the array_pop() function as well as the array_push() function. The way we're doing things now is a little simpler to work with but doesn't make it very clear where this model is being used. Would that help? :)
Customer: replied 8 months ago.
Yes very much so :) It's just that there expected to be there. Thank you
Expert:  chrisaw replied 8 months ago.
No worries - I'll get back to you shortly with an updated build including both array_pop and array_push. This does require restructuring the script though so may be another 30mins or so. I'll send the script over once complete.
Expert:  chrisaw replied 8 months ago.
Ok - I have managed to get this working in a rather elegant way. The code now uses the push / pop stacks model you described and stores two cookies now: prev_pages (a stack of previous pages)next_pages (a stack of next pages) The items shift between these stacks as they are navigated (so going *forward* will pop from next_pages and push to prev_pages, going *backward* will pop from prev_pages and push to next_pages.) All of that logic is present in each of the sql1-sql10 files within the attached ZIP. I have also cleaned the code up a bit and added more commenting to hopefully make it easier to understand what's going on at each stage of the code. Please let me know if you have any further queries! Christopher
Customer: replied 8 months ago.
Hey Christopher. Could you maybe explain where about in the code the functions of Push and Pop are used and how they work please?
Expert:  chrisaw replied 8 months ago.
Sure thing. In sql*.php: This will push the last page in the prev_pages stack to be the last page in the next_pages stack:[Line 17] array_push($next_pages, array_pop($prev_pages)); This will push the last page in the next_pages stack to be the last page in the prev_pages stack:[Line31] array_push($prev_pages, array_pop($next_pages));
Customer: replied 8 months ago.
Hello again. I think where nearly done here and I have just one more question. Could you explain to me how PHP arrays and get variables work in this so I have better idea? After that I think we'll be all wrapped up :)
Expert:  chrisaw replied 8 months ago.
Hi again! Ok so got the $_GET[] variables - all of those have been removed in this improvement since they aren't necessary with the new approach of using two stacks. As for the arrays - the term "array" and "stack" are interchangeable in this context so essentially: - When you click "next page" that page is pulled from the next_pages stack (or array) and added to the prev_pages stack (or array.)- When you click "prev page" that page is pulled from the prev_pages stack (or array) and added to the next_pages stack (or array.) I suspect your lecturer is calling them stacks to make them easier to understand how you are intending to use them (i.e. removing from the bottom and pushing to the bottom of each stack. Hope that clears things up a bit and I'm glad we were able to find a solution that worked and fit your exact criteria. Sorry it took a little while to get my head around exactly what you were looking for in terms of how this needed to function to fit your requirements. :) Best of luck with your course and feel free to raise further questions if you're finding anything you could use a hand with - I'm always happy to help! Christopher

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
 
 
 

Meet The Experts:

 
 
 
  • Steve Herrod

    Steve Herrod

    Computer Support Specialist

    Satisfied Customers:

    160
    Familiar with a wide variety of software and experienced in user training/support
< Previous | Next >
  • http://ww2.justanswer.com/uploads/SP/spherrod/2012-6-6_174244_1000852.64x64.JPG Steve Herrod's Avatar

    Steve Herrod

    Computer Support Specialist

    Satisfied Customers:

    160
    Familiar with a wide variety of software and experienced in user training/support
  • http://ww2.justanswer.com/uploads/SE/Seashore2011/2011-12-6_33418_111205223015.64x64.jpg Chris L.'s Avatar

    Chris L.

    Support Specialist

    Satisfied Customers:

    114
    Certified Software expert with over 10 years experience.
  • http://ww2.justanswer.com/uploads/JE/jessmagz/2012-6-6_18129_jm.64x64.jpg Jess M.'s Avatar

    Jess M.

    Computer Support Specialist

    Satisfied Customers:

    112
    Computer Software Support specialist for more that 10 years
  • http://ww2.justanswer.com/uploads/FS/fszcze/2012-6-18_181848_500test.64x64.jpg Frederick S's Avatar

    Frederick S

    Computer Specialist

    Satisfied Customers:

    91
    Computer technician; founder of a home PC repair company.
  • http://ww2.justanswer.com/uploads/WA/wa94sound/2012-6-6_202633_iStock000007010121Small.64x64.jpg Bernie's Avatar

    Bernie

    Software Support

    Satisfied Customers:

    52
    6+ years experience, Microsoft MCDST/CompTia A+ Certified
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Software Engineer

    Satisfied Customers:

    38
    20 years experience providing remote computer support
  • http://ww2.justanswer.com/uploads/jnayes/2010-12-23_132454_japicbetter.jpg jnayes's Avatar

    jnayes

    Computer Support

    Satisfied Customers:

    37
    Network Administrator