zenix Posted May 30, 2009 Share Posted May 30, 2009 Hi, apparently I am missing something some place. I have been playing around/experimenting to sharpen my php skills and better understand the language. I made a (what was supposed to be a) very simple log in form and a welcome page. On the welcome page I have it set to state what time the person logged in. This is only a pretend page solely for the purpose of me learning. If someone could set me straight I'd REALLY appreciate it. Here is the log in page code: <?php //Turn on output buffering ob_start(); ?> <!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" /> </head> <body> <?php ini_set('display_errors', 1); error_reporting(E_ALL &~E_NOTICE); //Set page title define ('TITLE', 'Login'); require ('templates/header.htm'); print '<div id ="leftcontent"> <h1>Login Form</h1> <p> Please log in to access your account</p>'; //check if form submitten if(isset($_POST['submit'])) { //handle the form if((!empty($_POST['uname']))&&(!empty($_POST['pword']))) { if(($_POST['uname']=='testing')&&($_POST['pword']=='test')) { //session stuff session_start(); $_SESSION['uname'] = 'testing'; $_SESSION['loggedin'] = time(); //Redirect to the welcome page header('location: welcome2.php'); exit(); } else { print '<p>The user name and/or password is not right.</p>'; } } else { print '<p>Please make sure all the fields are fill out.</p>'; } } else { //display form print'<form action ="welcome2.php" method ="post"><p> <br/> User name: <input type="text" name ="uname" size ="30"/> <br/> Password: <input type ="password" name ="pword" size ="30"/> <br/> <br/> Favorite Color: <input type ="text" name ="color" size ="15" /><br/> <br/> Age: <input type ="text" name ="age" size ="3" /><br/> <input type ="submit" name ="submit" value ="Log me in!"/></p> </form>'; } print '<div>'; require ('templates/footer.htm'); //Turn off buffering ob_end_flush(); ?> </body> </html> And the welcome page code: <?php //Turn on output buffering ob_start(); ini_set('display_errors', 1); error_reporting(E_ALL & ~E_NOTICE); //start session session_start(); define('TITLE', 'Welcome to the Zenix Computers test site!'); require ('templates/header.htm'); //print greeting print '<div id="leftcontent"> <h1>Welcome to the Zenix Computers test site, ' . ucfirst($_SESSION['uname']) . '!</h1>'; //if (isset($_SESSION['loggedin'])) //{ //print how long they've been logged in print '<p>You have been logged in since:<br/> ' . date ('g:i A', $_SESSION['loggedin']) . '</p>'; //} //else //{ //header('location: login.php'); //} //Make log out link print '<p><a href ="logout.php">Click here to log out</a></p>'; print '</div>'; require( 'templates/footer.htm'); //Turn off buffering ob_end_flush(); ?> I left everything I did to figure out what was going wrong. The commented out code starting with if (isset... was placed there to verify if any session information was being sent to the page at all. Before adding this code the welcome page always said that I've been logged in since 7:00 PM...even though it's only just after 10:AM. The footer is encoded to display the current date and time and it is correct there. I'd REALLY appreciate a little guidance! Thanks in advance!! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2009 Share Posted May 30, 2009 Output buffering should not be used to make your code work. It should only be used to do things like capture output that you then want to do something with in your application. Organize your code so that any logic that needs to use headers (session_start(), header()...) comes first on the page, then produce and output content only if you are going to stay on that page. Use error_reporting(E_ALL); By turning off the E_NOTICE reporting you are hiding errors that would help point out why your code is not working, such as typo's in variable names, form fields that don't have any values, session variables that don't have any value, function calls that don't have expected parameters... Edit: and based on the action="..." parameter of your form, your form submits to a page that is probably different than the first code posted. Is the first code welcome2.php? and if so, it is redirecting to itself when login is successful. Edit2: You should probably not put the error_reporting/display_errors settings in all your files. They should only be put in for debugging purposes in a specific file you are having problems with. You should set these globally on a development system in the master php.ini, in a .htaccess file, or in a local php.ini, so that you don't need to go through all your files and remove them when you are done. Quote Link to comment Share on other sites More sharing options...
zenix Posted May 30, 2009 Author Share Posted May 30, 2009 Thank you for the reply. I did away with the output buffering and the &~E_NOTICE. I get warnings saying that uname and logged in are undefined indexes. uname is defined in the html below. <?php //Turn on output buffering //ob_start(); ini_set('display_errors', 1); error_reporting(E_ALL); //start session session_start(); define('TITLE', 'Welcome to the Zenix Computers test site!'); require ('templates/header.htm'); //print greeting print '<div id="leftcontent"> <h1>Welcome to the Zenix Computers test site, ' . ucfirst($_SESSION['uname']) . '!</h1>'; //if (isset($_SESSION['loggedin'])) //{ //print how long they've been logged in print '<p>You have been logged in since:<br/> ' . date ('g:i A', $_SESSION['loggedin']) . '</p>'; //} //else //{ //header('location: login.php'); //} //Make log out link print '<p><a href ="logout.php">Click here to log out</a></p>'; print '</div>'; require( 'templates/footer.htm'); //Turn off buffering //ob_end_flush(); ?> The first bloack of code I provided the first time is the log in page, the second is the welcome2 page. This is the modified welcome2.php page that is throwing the warnings. Thanks again! Quote Link to comment Share on other sites More sharing options...
zenix Posted May 30, 2009 Author Share Posted May 30, 2009 I forgot about moving the error reporting code. Thanks for the heads up on this though, I will not make it a practice to have it there all the time. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2009 Share Posted May 30, 2009 Your login form is submitting directly to welcome2.php, so the session variables are not being set by the form processing code that is in the first piece of code posted. So, yes those two session variables are undefined when welcome2.php is executed and the 7:00 AM time is because you are feeding the date() function a null value for $_SESSION['loggedin']. Quote Link to comment Share on other sites More sharing options...
zenix Posted June 1, 2009 Author Share Posted June 1, 2009 Please excuse my extreme ignorance; how can I fix it? I'd really appreciate it if you could guide me just a little. Thanks a lot for your reply! Quote Link to comment Share on other sites More sharing options...
zenix Posted June 1, 2009 Author Share Posted June 1, 2009 I experimentd a little, as I AM trying to learn here and changed my code in the log in page to include $_SESSION['loggedin'] = date ('g:i A');. Rather than a null Time() statement. It appears that I was srong, because nothing changed. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 1, 2009 Share Posted June 1, 2009 The following is your <form tag - print'<form action ="welcome2.php" method ="post"><p> The action="..." attribute is where the form data is submitted to. Your action attribute value is welcome2.php. That is not the first piece of code where your form processing code is that is checking if the 'uname' and 'pword' are correct and setting the $_SESSION variables. Quote Link to comment Share on other sites More sharing options...
zenix Posted June 1, 2009 Author Share Posted June 1, 2009 I DID IT!! YAY!! Thank you so much for your help! Here is what I did. My log in code looks like this: <?php ob_start(); session_start(); //set page title and include header file define('TITLE', 'login2'); require('templates/header.htm'); print '<div id ="leftcontent"> <h1>Log in form</h1> <p> Please log in to your account</p>'; //check if form has been submitted if(isset($_POST['submit'])) { //handle form if((!empty($_POST['uname'])) && (!empty($_POST['pword']))) { if(($_POST['uname']=='tester') && ($_POST['pword'] == 'test')) { //Open session $_SESSION['uname'] = 'tester'; $_SESSION['loggedin'] = time('g:i A'); //redirect to welcome page header('location: welcome2.php'); exit(); //NOTHING IS BEING SENT TO THE WELCOME PAGE AFTER LOG IN } else { print '<p>Submitted log in not valid</p>'; } } else { //forgot a field print '<p>Please make sure all fields are completed</p>'; } } else { //Display the form print '<form action ="login2.php" method="post"><p> User name:<input type="text" name ="uname" size ="20"/> <br/> Password: <input type ="password" name ="pword" size ="20"/><br/> <input type="submit" name ="submit" value ="Submit my info!"/></p></form>'; } //complete formatting print '</div>'; require ('templates/footer.htm'); ob_end_flush(); ?> My welcome page code is this: <?php //start session session_start(); //Turn on output buffering //ob_start(); ini_set('display_errors', 1); error_reporting(E_ALL); define('TITLE', 'Welcome to the Zenix Computers test site!'); require ('templates/header.htm'); //print greeting print '<div id="leftcontent"> <h1>Welcome to the Zenix Computers test site, ' . ucfirst($_SESSION['uname']) . '!</h1>'; //print how long they've been logged in print '<p>You have been logged in since:<br/> ' . date ('g:i A', $_SESSION['loggedin']) . '</p>'; //Make log out link print '<p><a href ="logout.php">Click here to log out</a></p>'; print '</div>'; require( 'templates/footer.htm'); ?> This site (and you) are awesome! Quote Link to comment 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.