Gldnbr Posted February 18, 2011 Share Posted February 18, 2011 How do I make it so if I get the question right, I have access to view the next page? For example, if I get this question right in index-1.php correct, it would take me to index-2.php (like in the script below), but if you don't get the question correct in index-1.php, you cannot have access/view index-2.php. Why I need this? Well, if you change the URL http://--------/index-1.php to http://--------/index-2.php, you can easily go from index-1.php to index-2.php without having to answer the question correctly. index-1.php: <?php if(isset($_POST['submit'])){ $number = $_POST['number']; if ($number == "elephant"){ header("Location: http://localhost/index-2.php"); exit();} } ?> <html> <head> <title>PHP Test</title> </head> <b>LEVEL 1</b> <body> <p> <font face="Courier New">c291cmNl</font> </p> <br/></body> </html> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> Answer: <!-- "elephant" --> <input type="text" name="number" /><br /> </select> <input name="submit" type="submit"> </form> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/228044-special-restriction-of-some-sort/ Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2011 Share Posted February 18, 2011 You would use a $_SESSION variable to 'remember' the last correct question/page and then test that session variable on the next page to determine what happens on that page (redirect back to the previous page or display the expected content for the page.) Quote Link to comment https://forums.phpfreaks.com/topic/228044-special-restriction-of-some-sort/#findComment-1175907 Share on other sites More sharing options...
RestlessThoughts Posted February 18, 2011 Share Posted February 18, 2011 Edit: Oops, I'm too slow. Use sessions to keep track of where the user is at. If they haven't answered the question right, send them back to the previous question. <?php session_start(); if(isset($_POST['submit'])){ $number = $_POST['number']; if ($number == "elephant"){ $_SESSION['question'] = 2; header("Location: http://localhost/index-2.php"); exit();} } ?> Then on index-2: <?php session_start(); if (!isset($_SESSION['question']) OR $_SESSION['question'] != 2){ header("Location: http://localhost/index-1.php"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228044-special-restriction-of-some-sort/#findComment-1175910 Share on other sites More sharing options...
Gldnbr Posted February 18, 2011 Author Share Posted February 18, 2011 Okay I see that now. This time I am trying to do the same for index-2.php to index-3.php, anyone know what is up? ERROR: Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\index-2.php:10) in C:\xampp\htdocs\index-2.php on line 17 index-2.php: <?php session_start(); if (!isset($_SESSION['question']) OR $_SESSION['question'] != 2){ header("Location: http://localhost/index-1.php"); } ?> <?php session_start(); if(isset($_POST['submit'])){ $number = $_POST['number']; if ($number == "oil vial"){ $_SESSION['question'] = 3; header("Location: http://localhost/index-3.php"); exit();} } ?> <html> <head> <title>PHP Test</title> </head> <b>LEVEL 2</b> <body> <p> <font face="Courier New">vhwisgtpulffjroixfztpilnlixhjkmllwprkrmxyagrtonrcimeopytyioavbvykivnyqgxvzibjxwjzvmkdbhpwlbofudzpmys</font> </p> <br/></body> </html> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> Answer: <input type="text" name="number" /><br /> </select> <input name="submit" type="submit"> </form> index-3.php: <?php session_start(); if (!isset($_SESSION['question']) OR $_SESSION['question'] != 3){ header("Location: http://localhost/index-2.php"); } ?> <?php session_start(); if(isset($_POST['submit'])){ $number = $_POST['number']; if ($number == "red hot chili peppers"){ $_SESSION['question'] = 4; header("Location: http://localhost/index-4.php"); exit();} } ?> <html> <head> <title>PHP Test</title> </head> <b>LEVEL 3</b> <body> <p> <font face="Courier New">cmVkIGhvdCBjaGlsaSBwZXBwZXJz</font> </p> <br/></body> </html> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> Answer: <input type="text" name="number" /><br /> </select> <input name="submit" type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/228044-special-restriction-of-some-sort/#findComment-1175919 Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2011 Share Posted February 18, 2011 output started at C:\xampp\htdocs\index-2.php:10 (line 10) You are sending output to the browser at line 10 in your file. I'll guess it is the blank line inbetween your ?> and <?php tags - ?> <----------- this is a line that is outside of your php code and will be sent to the browser. <?php You also need an exit; statement after your header() redirect, because anyone who wanted to view your page can just ignore the header() redirect that is being sent to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/228044-special-restriction-of-some-sort/#findComment-1175925 Share on other sites More sharing options...
Gldnbr Posted February 18, 2011 Author Share Posted February 18, 2011 Okay thanks! Solved Quote Link to comment https://forums.phpfreaks.com/topic/228044-special-restriction-of-some-sort/#findComment-1175940 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.