colleyboy Posted January 11, 2011 Share Posted January 11, 2011 Hi there, I have a simple login script written but I get an error with it. It does work but shows an error on some pages. Let me explain. Three Files: Admin.php Login.html checklogin.php When the user has logged in they go to checklogin.php. If the username and password match 1 row in the database then it forwards the user to admin.php fine. Except I keep getting mysql warning messages: Warning: Cannot modify header information - headers already sent by (output started at /home/wormste1/public_html/tilburywebdesign/shop/templates/template1/admin/updatescompanyinformation.php:3) in /home/wormste1/public_html/tilburywebdesign/shop/templates/template1/admin/companyinfoupdated.php on line 3 At the start of each page I want password protected I put the following code: <? session_start(); if(!session_is_registered(myusername)){ header("location:login.html"); } ?> I can't work out why I am getting this error. Many Thanks, Ian Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2011 Share Posted January 11, 2011 output started at . . . /updatescompanyinformation.php:3 ^^^ Something at or up to line 3 in that file is sending output to the browser. Also, read the following post as to why the code you are putting on each page is out of date and is not secure - http://www.phpfreaks.com/forums/php-coding-help/forms-gone-crazy/msg1510368/#msg1510368 Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157837 Share on other sites More sharing options...
colleyboy Posted January 11, 2011 Author Share Posted January 11, 2011 Thanks for that, I replaced the code in admin.php with : This is now at the start of all 'protected' pages: <? session_start(); if(!isset($_SESSION['username'])){ header("location:login.html"); exit; // prevent access to all the rest of the code on the page } ?> but now when I login it doesnt stay on the protected page instead it redirects back to the login.html any idea why? :S Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157839 Share on other sites More sharing options...
colleyboy Posted January 11, 2011 Author Share Posted January 11, 2011 This is the code for checklogin.php if that helps: <?php $host="localhost"; // Host name $username="xxxxxxxxxxx"; // Mysql username $password="xxxxxxxxxx"; // Mysql password $db_name="xxxxxxxxxxxxx"; // Database name $tbl_name="xxxxxxxxxxxxx"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "index.php" session_register("myusername"); session_register("mypassword"); header("location:index.php"); } else { echo "Wrong Username or Password"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157840 Share on other sites More sharing options...
colleyboy Posted January 11, 2011 Author Share Posted January 11, 2011 Oh no... The login itself is still working... but I still get the same error: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/wormste1/public_html/tilburywebdesign/shop/templates/template1/admin/updatescompanyinformation.php:3) in /home/wormste1/public_html/tilburywebdesign/shop/templates/template1/admin/companyinfoupdated.php on line 2 This is line 2: <? session_start(); <--------------- LINE 2 if(!isset($_SESSION['myusername'])){ header("location:login.html"); exit; // prevent access to all the rest of the code on the page } ?> Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157841 Share on other sites More sharing options...
colleyboy Posted January 11, 2011 Author Share Posted January 11, 2011 by taking out session.start on each page it seems to still give an error pointing towards another line? Am well confused.com! Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157848 Share on other sites More sharing options...
dragon_sa Posted January 11, 2011 Share Posted January 11, 2011 your errors are pointing to this page updatescompanyinformation.php whats in that page the second page error is a result of the first Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157852 Share on other sites More sharing options...
colleyboy Posted January 11, 2011 Author Share Posted January 11, 2011 Ah-ha... success... Many Thanks... I started the php coding but left 3 lines blank before the <? so it got confused ...Sorted Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/224073-problem-with-a-simple-login-script/#findComment-1157854 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.