ralphy45 Posted July 25, 2010 Share Posted July 25, 2010 Hi, As I mentioned in my intro, I've just started learning PHP. I'm trying to write a script which will have three visible pages - one is asking for the user's name, and which page they would like to visit. The other two will be the pages which they can select from the first page, and a message saying hello. I've got this working through the session tag, but only when the form code is set to go a particular page. To allow the user to select, I've added a non-visible page, called redirect.php, which takes the value selected in the first page (the drop-down, for which page they'd like to visit), and sends them to that page. When I go through the redirect page, the session doesn't appear to 'keep' the user's name, entered on the first page. The redirect script seems to work okay, but the variable containing the username seems to be blank in the final script. Can someone advise where I'm going wrong? The three scripts are below: Starting Page: <?php session_start(); $_SESSION[‘uname’] = “testing”; ?> <html> <head> </head> <body> <?php echo "<form action='redirect.php' method='post'> <p><input type='text' name='uname'><br> <select name='gourl'> <option value=''>Choose a Destination... <option value='test2.php'>Test Page 2 <option value='test3.php'>Test Page 3 </select> <input type='submit' value='Go'> </form>"; ?> </body></html> Redirect Script: <?php $url = $_POST["gourl"]; header("Location: $url"); ?> Final Page: <html><body> <?php $testinghere = $_SESSION['uname']; echo "Hello, $testinghere \n"; ?> </body></html> I've tried loads of small edits, but like I said, I'm a beginner, so any expert advice would be very much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/ Share on other sites More sharing options...
Tazerenix Posted July 25, 2010 Share Posted July 25, 2010 you need to put session_start() into your final page also, before <html> as well as it send headers if you have session.use_cookies enabled i believe Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090859 Share on other sites More sharing options...
ralphy45 Posted July 25, 2010 Author Share Posted July 25, 2010 you need to put session_start() into your final page also, before <html> as well as it send headers if you have session.use_cookies enabled i believe Hey, Thank you for your help. I've just tried adding the session_start line, but still the variable value is not shown on the final page. Can you see anything else wrong? Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090867 Share on other sites More sharing options...
magnetica Posted July 25, 2010 Share Posted July 25, 2010 Be sure that the session_start() is situated right at the beginning of the document except before the <?php tag but before any whitespace or declared variables etc. Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090870 Share on other sites More sharing options...
ralphy45 Posted July 25, 2010 Author Share Posted July 25, 2010 Hi Magnetica, In the final page, I've added it right at the start, before the <html> tag, in this form: <?php session_start(); ?> Is that right? Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090876 Share on other sites More sharing options...
webmaster1 Posted July 25, 2010 Share Posted July 25, 2010 Here's a tested solution: a.php: <?php session_start(); if (isset($_POST['submit'])) { $_SESSION['uname'] = $_POST["uname"]; $url = $_POST["gourl"]; header("Location: $url"); } ?> <html> <head> </head> <body> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'> <p><input type='text' name='uname'><br> <select name='gourl'> <option value=''>Choose a Destination...</option> <option value='b.php'>Page B</option> </select> <input type='submit' value='Go' name='submit'> </form> </body></html> b.php: <?php session_start(); ?> <html><body> <?php $testinghere = $_SESSION['uname']; echo "Hello, $testinghere \n"; ?> </body></html> You'll need to input a value into the uname field to see the session echo on the next page. Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090879 Share on other sites More sharing options...
webmaster1 Posted July 25, 2010 Share Posted July 25, 2010 As the lads have said, remember that session_start(); needs to be used to continue sessions as much as creating them. Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090881 Share on other sites More sharing options...
Pikachu2000 Posted July 25, 2010 Share Posted July 25, 2010 In the OP, the quotes are the wrong type on this line. Not that they look odd. $_SESSION[‘uname’] = “testing”; Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1090998 Share on other sites More sharing options...
ralphy45 Posted July 25, 2010 Author Share Posted July 25, 2010 webmaster1, that's working like a charm, thank you to all for your help with this, very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1091038 Share on other sites More sharing options...
Pikachu2000 Posted July 25, 2010 Share Posted July 25, 2010 In the OP, the quotes are the wrong type on this line. Not that they look odd. $_SESSION[‘uname’] = “testing”; Make that last line NOTE that they look odd . . . Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1091048 Share on other sites More sharing options...
webmaster1 Posted July 26, 2010 Share Posted July 26, 2010 webmaster1, that's working like a charm, thank you to all for your help with this, very much appreciated. Glad to help. Quote Link to comment https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/#findComment-1091492 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.