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, DevOps Manager
Category: Software
Satisfied Customers: 13
Experience:  DevOps Manager with over 10 years experience in the DevOps and IT field.
Type Your Software Question Here...
chrisaw is online now
This answer was rated:

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

will allow to get started... Show More
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.
Show Less
Ask Your Own Software Question
Customer reply replied 1 year ago.
Customer reply replied 1 year ago.
I have added the specificition. Question i want answered starts at 3B. Thank you
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!
If the formatting was broken on that - I've added this to a pastebin link:
Customer reply replied 1 year 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.
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 reply replied 1 year ago.
By stacks I mean by PUSH, POP and TOP. There our essential for the project. Would this code be using these?
Customer reply replied 1 year 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.
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 reply replied 1 year 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 reply replied 1 year ago.
I hope i'm not coming across rude for asking this but again thank you.
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 reply replied 1 year 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.
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.
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 reply replied 1 year 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.
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
Customer reply replied 1 year 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 reply replied 1 year 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
No hassle at all - I'll have a look at that now and get back to you very shortly.
Customer reply replied 1 year ago.
Thank you. Here's the spec again if you need it.
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 reply replied 1 year 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.
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 reply replied 1 year ago.
Well hyper link buttons are needed to help navigate through the pages. Sorry if you didn't know that.
Customer reply replied 1 year ago.
But i'm sure your doing a great job
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 reply replied 1 year 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.
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 reply replied 1 year 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 :)
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 reply replied 1 year 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
Sure thing. Just drop me a response if you have any further questions. :)
Customer reply replied 1 year 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?
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 reply replied 1 year 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?
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 and reword them a bit. ;)
Customer reply replied 1 year 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!
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 reply replied 1 year 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
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 reply replied 1 year ago.
Yes very much so :) It's just that there expected to be there. Thank you
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.
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 reply replied 1 year 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?
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 reply replied 1 year 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 :)
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