websponge Posted July 28, 2010 Share Posted July 28, 2010 Im pretty new to PHP, but have managed to create a login system for a small site, my question relates to why Im entering a certain piece of code, I understand the code apart from the getting the information from a form and assigning it to a variable, I have a username form with one submit button, (username) which goes to a script called register.php I have this in my php: <?php error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); $username=$_POST['username']; session_register("username"); header("location:index2.php"); ?> then in my index2.php page i have this: <?php error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); session_start(); $username= $_SESSION['username']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Page</title> </head> <body> <p> Hi <?php echo $username; ?> </p> </body> </html> Ive simplified what ive done here,why am I declaring a variabel to get the form contents? cant I just get the contents? and then register? I dont need a variable.. If I remove the line, it doesnt work obviously.. thanks Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/ Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 The POST data from the form is no longer available once you redirect the page. That is why you must store it in a session variable if you want to reuse it later. You shouldn't be using session_register either, it's depreciated. Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092085 Share on other sites More sharing options...
aleX_hill Posted July 28, 2010 Share Posted July 28, 2010 Get your form to submit to register.php (like you do) then you should be able to access the username just by using $_POST['username'] if the method for the form is post, and $_GET['username'] if it is set to get. You could do the processing (in this case echoing) on that file, or get your form to submit straight to index2.php and have switch to check if the form was submitted, and if so then do the processing there. There is no real need to set the variable as a session and redirect to a new page. The above code never sets $_SESSION['username'] anywhere, just registers the "username" session Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092090 Share on other sites More sharing options...
websponge Posted July 28, 2010 Author Share Posted July 28, 2010 Thats what I dont understand, I never ask to use that variable again, I have to declare a new one in index2.php with this line: $username= $_SESSION['username']; I dont know why I have to do it twice if you like? sorry its a dull question.. once I get the post details, can I register it on register.php and not do it again on index2.php Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092094 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 Once the page is redirected variables from the previous page are no longer available. That is why you store variables in sessions in the first place, so you're able to transfer variables across pages. Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092095 Share on other sites More sharing options...
websponge Posted July 28, 2010 Author Share Posted July 28, 2010 Once the page is redirected variables from the previous page are no longer available. That is why you store variables in sessions in the first place, so you're able to transfer variables across pages. Thats my understanding too, so why bother declaring it in the register.php page if it wont get passed to the next page? Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092098 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 So that it would be defined by session_register, which you shouldn't be using because, as I stated before, it's depreciated. Instead you should do this: <?php error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); $_SESSION['username'] = $_POST['username']; header("location:index2.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092100 Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2010 Share Posted July 28, 2010 On your first page (every page that sets or references a $_SESSION variable), you need a session_start() statement. Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092102 Share on other sites More sharing options...
websponge Posted July 28, 2010 Author Share Posted July 28, 2010 So that it would be defined by session_register, which you shouldn't be using because, as I stated before, it's depreciated. Instead you should do this: <?php error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); $_SESSION['username'] = $_POST['username']; header("location:index2.php"); ?> Tried that, it doesnt display the username, perhaps this is too much for me to learn Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092110 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 As PFMaBiSmAd said, I forgot to include session_start. <?php error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); session_start(); $_SESSION['username'] = $_POST['username']; header("location:index2.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092111 Share on other sites More sharing options...
websponge Posted July 28, 2010 Author Share Posted July 28, 2010 On your first page (every page that sets or references a $_SESSION variable), you need a session_start() statement. that did it! thanking you, didnt realise the session had to be started before everything! thanks both! Quote Link to comment https://forums.phpfreaks.com/topic/209103-simple-session-question/#findComment-1092112 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.