Grantel Posted September 4, 2012 Share Posted September 4, 2012 After 8 hours of beating my head against whatever was close I have come for help. I searched the forums, but as this is my 3rd day using PHP i am unsure of the proper verbage to use. What I am trying to accomplish is basically a "quiz" for my customers. They enter their first name, last name, and month into a field and that will create a text file that I will then put/grab that data from later. I have completely simplified everything down to just a single variable being stored in the session so I can understand whats going on. My problem comes about when I try to move from page to page keeping the file name constant. My variable, which will not change from any page. This is why I believe I want a $_session. My session is following me to the second page. I have tried the following with server globals on and off. Who will stop my head from bleeding more? <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <?php session_start(); if (isset($_POST['Submit'])) { $_session['UserName'] = $_POST['firstname']; } ?> <form action=addressgoeshere.php method="POST"> Your First Name:<INPUT TYPE="TEXT" NAME="firstname" size="35"> <body> </body> </html> The second page "addressgoeshere.php" <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <?php session_start(); echo "The username is:"; echo $_session['UserName']; echo "..."; ?> <body> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/ Share on other sites More sharing options...
Barand Posted September 4, 2012 Share Posted September 4, 2012 $_SESSION, not $_session. PHP variables are case-sensitive Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1374987 Share on other sites More sharing options...
Grantel Posted September 4, 2012 Author Share Posted September 4, 2012 I very much appreciate the quick response. Case sensitivity matched. Still not working. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1374996 Share on other sites More sharing options...
Pikachu2000 Posted September 4, 2012 Share Posted September 4, 2012 You need to enable error reporting, and also read the sticky thread at the top of this forum titled, "HEADER ERRORS - READ HERE BEFORE POSTING". Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1374998 Share on other sites More sharing options...
Monkuar Posted September 4, 2012 Share Posted September 4, 2012 Wheres your <input type="submit" name="Submit" value="Submit"> button? I recommend learning a bit more html as well, Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375000 Share on other sites More sharing options...
Grantel Posted September 4, 2012 Author Share Posted September 4, 2012 You need to enable error reporting, and also read the sticky thread at the top of this forum titled, "HEADER ERRORS - READ HERE BEFORE POSTING". Ok, read the header errors... not sure I learned much but as im learning im sure to reference it again. So i enabled error reporting. The username is: Notice: Undefined index: UserName in WEBADDRESSOFHTMLFILEHERE on line 14 ... 14. echo $_SESSION['UserName']; Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375016 Share on other sites More sharing options...
Christian F. Posted September 4, 2012 Share Posted September 4, 2012 That's not the only error you're getting, unless you've changed your code after reading the article about header errors? Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375019 Share on other sites More sharing options...
Grantel Posted September 4, 2012 Author Share Posted September 4, 2012 That's not the only error you're getting, unless you've changed your code after reading the article about header errors? You are correct. I have changed my code in an attempt to follow the direction of the previous help. <html xmlns="http://www.w3.org/1999/xhtml"> <?php ob_start(); session_name("ThisSession"); session_start(); ini_set('display_errors',1); error_reporting(E_ALL); $_SESSION['UserName'] = "asd"; if (isset($_POST['Submit'])) { $_SESSION['UserName'] = $_POST['firstname']; $url = 'targetfileishere'; echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">'; } ob_end_flush(); ?> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action=addresshere/index.phtml method="POST"> Your First Name:<INPUT TYPE="TEXT" NAME="firstname" value="firstname" size="35"> <input type="submit" name="Submit" value="Submit"> </body> </html> Page 2 now <!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>Untitled Document</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <?php session_name("ThisSession"); session_start(); ini_set('display_errors',1); error_reporting(E_ALL); echo "The username is:"; echo $_SESSION['UserName']; echo "..."; ?> </body> </html> And the Notice given The username is: Notice: Undefined index: UserName in FILELOCATIONHERE on line 17 ... Line 17 - echo $_SESSION['UserName']; Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375027 Share on other sites More sharing options...
Christian F. Posted September 4, 2012 Share Posted September 4, 2012 You're still not doing it correctly, and that notice is not the only message you should be given. I recommend re-reading the HEADER ERRORS article, and also take a look in the PHP manual for header (); The warning in the description is true for session_start () as well, as they both send out HTTP headers. Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375033 Share on other sites More sharing options...
Pikachu2000 Posted September 4, 2012 Share Posted September 4, 2012 You should be getting a "headers already sent" notice due to the output being sent to the browser before session_start(). Output buffering is pretty much a hack, and you should really avoid using it. This code is certainly fixable without output buffering. Your error reporting directives in php.ini should be as follows: error_reporting = -1 display_errors = On Then after saving the file, restart Apache. Or for the time being, you can add the following at the head of your scripts, right after the opening php tag: error_reporting(-1); ini_set('display_errors', 'On'); Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375039 Share on other sites More sharing options...
Grantel Posted September 4, 2012 Author Share Posted September 4, 2012 You're still not doing it correctly, and that notice is not the only message you should be given. I recommend re-reading the HEADER ERRORS article, and also take a look in the PHP manual for header (); The warning in the description is true for session_start () as well, as they both send out HTTP headers. Got it solved. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/267972-sigh-very-basic-session-help-if-you-would-be-so-kind/#findComment-1375042 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.