strujillo Posted August 7, 2008 Share Posted August 7, 2008 Ok so here is whats going on. I want to put something in my code that does the following. When you log in, every page you visit on the website after that checks to make sure you are logged in first. thats it and its killing me. So i went to phpeasystep.com (this website bugs the heck out of me, but its ok on some stuff) and looked at their login script tutorial. http://phpeasystep.com/phptu/6.html. So i based alot of my stuff off of this...like connecting to the database and stuff. Anyways, here is some of my code. This is the INDEX page. It takes the basic info and sends it to check_login to see if they are a valid user. <!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=iso-8859-1" /> <title>ABHS Online Community</title> </head> <body> <!--This FORM creates the table that containst he login password and username boxes Help from::: http://phpeasystep.com/workshopview.php?id=6 --> <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <!--Once you click login you will be redirected to login_check.php to see if you have logged in correctly--> <form name="login" method="post" action="login_check.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="username" type="text" size="16" maxlength="25" /></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="password" type="password" size="16" maxlength="25" /></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"><input name="register" type="button" value="Register"/></td> </tr> </table> </td> </form> </tr> </table> </body> </html> This code checks to see if they are valid users. dbconnect.php below <?php ob_start(); include('dbconnect.php'); //$result = mysql_query("SELECT * FROM members") or die(mysql_error()); $myusername = $_POST['username']; $mypassword = $_POST['password']; $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); //checking to see if this information excists. $sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); //Finds how many results there are. THere can only be one otherwise it wont work. $count=mysql_num_rows($result); if($count==1){ //logged in successful. Go to members home. session_register("myusername"); session_register("mypassword"); echo "<a href=members_home.php>Click me to make the login process final!</a>"; } else { // if the logg in information is incorrect, it displays this. echo "Wrong Username or Password</br>"; echo"<a href=index.php>Try Again?</a>"; } ob_end_flush(); ?> connects to database <?php //Connects to database using the following login info $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; //Logs in to database. Creates a connection, using information. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); //Opens the database abhs $dbname = 'abhs'; mysql_select_db($dbname); ?> and finally the members page. See when i get to the members page i want it to check to make sure this person is logged in. I dont want just anyone to have access to this, THEY MUST BE LOGGED IN. <!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=iso-8859-1" /> <title>Members Home</title> </head> <body> <? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> Login Successful <a href="http://localhost/abhs/login_check.php">Check that your still logged in.</a> <? phpinfo(); ?> </body> </html> So when i get here it produces this error. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\abhs\members_home.php:9) in C:\xampp\htdocs\abhs\members_home.php on line 10 i have PHP Version 5.2.6 PLEASE HELP!!! this is for my school and im stressing out. Link to comment https://forums.phpfreaks.com/topic/118572-help-with-this-php-login-script-its-killing-me/ Share on other sites More sharing options...
abdfahim Posted August 7, 2008 Share Posted August 7, 2008 move this portion at the top of your page <? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> you can never use session_start() after you echo something or put any HTML tag. Link to comment https://forums.phpfreaks.com/topic/118572-help-with-this-php-login-script-its-killing-me/#findComment-610455 Share on other sites More sharing options...
strujillo Posted August 7, 2008 Author Share Posted August 7, 2008 yeah i read that just before i read your post. i did it but i when i went to see if i was still logged in, it said no. How do i make it to where i checks that im logged before anything else. and if im not sends me back to index.php Link to comment https://forums.phpfreaks.com/topic/118572-help-with-this-php-login-script-its-killing-me/#findComment-610456 Share on other sites More sharing options...
MasterACE14 Posted August 7, 2008 Share Posted August 7, 2008 try: <?php session_start(); if(!isset($_SESSION['myusername'])){ header("Location: main_login.php"); } ?> Link to comment https://forums.phpfreaks.com/topic/118572-help-with-this-php-login-script-its-killing-me/#findComment-610457 Share on other sites More sharing options...
abdfahim Posted August 7, 2008 Share Posted August 7, 2008 I am re-writing your 2nd section to simplify it <?php session_start(); if(isset($_POST['Submit'])){ include('dbconnect.php'); $myusername = $_POST['username']; $mypassword = $_POST['password']; $result = mysql_query("SELECT `username` FROM members WHERE `username`='".$myusername."' AND `password`='".$mypassword."'") or die(mysql_error()); if(mysql_num_rows($result)>0){ $SESSION['myusername']=$myusername }else { echo "Wrong Username or Password</br>"; echo"<a href=index.php>Try Again?</a>"; } } ?> Then in the top of every restricted page, put <? session_start(); if(!isset($_session['myusername'])){ header("location:main_login.php"); } ?> Link to comment https://forums.phpfreaks.com/topic/118572-help-with-this-php-login-script-its-killing-me/#findComment-610461 Share on other sites More sharing options...
strujillo Posted August 7, 2008 Author Share Posted August 7, 2008 AWESOME!!! it was perfect!!! Thanks a bunches! Link to comment https://forums.phpfreaks.com/topic/118572-help-with-this-php-login-script-its-killing-me/#findComment-610464 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.