ZappyLawns Posted April 14, 2014 Share Posted April 14, 2014 I got my register to work. I got my login almost done but i cannot figure it out. Here are the codes and data bases try and see why login doesnt work. Tables: Config.php <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. 6) I DIDN'T MAKE THE VARIABLES FINAL ON PURPOSE! DON'T FLAME. */ //Information to connect to your MySQL Server AND DB $host = "localhost"; $username = "zappylaw_rob"; $password = "haha12"; $db = "zappylaw_zappylawns"; //Connect to MySQL Server $con = mysqli_connect($host,$username,$password,$db) or die("Can not connect to Server."); ?> Register.php <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. */ session_start(); //Must Start a session. require "config.php"; //Connection Script, include in every file! //Check to see if the user is logged in. //'isset' check to see if a variables has been 'set' if(isset($_SESSION['username'])){ header("location: members.php"); } //Check to see if the user click the button if(isset($_POST['submit'])) { //Variables from the table $user = $_POST['user']; $pass = $_POST['pass']; $rpass = $_POST['rpass']; //Prevent MySQL Injections $user = stripslashes($user); $pass = stripslashes($pass); $rpass = stripslashes($rpass); $user = mysqli_real_escape_string($con, $user); $pass = mysqli_real_escape_string($con, $pass); $rpass = mysqli_real_escape_string($con, $rpass); //Check to see if the user left any space empty! if($user == "" || $pass == "" || $rpass == "") { echo "Please fill in all the information!"; } else { //Check too see if the user's Passwords Matches! if($pass != $rpass) { echo "Passwords do not match! Try Again"; } //CHECK TO SEE IF THE USERNAME IS TAKEN, IF NOT THEN ADD USERNAME AND PASSWORD INTO THE DB else { //Query the DB $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user'") or die("Can not query the TABLE!"); //Count the number of rows. If a row exist, then the username exist! $row = mysqli_num_rows($query); if($row == 1) { echo "Sorry, but the username is already taken! Try again."; } //ADD THE USERNAME TO THE DB else { $add = mysqli_query($con,"INSERT INTO members (id, username, password) VALUES (null, '$user' , '$pass') ") or die("Can't Insert! "); echo "Successful! <a href='members.php'> Click Here </a> to log In."; } } } } ?> <html> <head> <title>Test</title> </head> <body style="background-color:#2E650D;"> <table style="margin-top:200px;"> <table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black"> <tr> <form name="register" method="post" action="register.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ccc"> <tr> <td colspan="3"><strong><center>Registration</center></strong></t d> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="user" type="text" id="user"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="pass" type="password" id="pass"></td> </tr> <tr> <td>Repeat Password</td> <td>:</td> <td><input name="rpass" type="password" id="rpass"></td> </tr> <tr> <td></td> <td></td> <td><input type="submit" name="submit" value="Register"></td> </tr> </table> </td> </form> </tr> </table> </table> </body> </html> login.php: <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. */ session_start(); require "config.php"; //Connection Script, include in every file! //Check to see if the user is logged in. if(isset($_SESSION['username'])){ header("location: members.php"); //isset check to see if a variables has been 'set' } if(isset($_POST['submit'])) { //Variables from the table $user = $_POST['user']; $pass = $_POST['pass']; //Prevent MySQL Injections $user = stripslashes($user); $pass = stripslashes($pass); $user = mysqli_real_escape_string($con, $user); $pass = mysqli_real_escape_string($con, $pass); //Check to see if the user left any space empty! if($user == "" || $pass == "") { echo "Please fill in all the information!"; } //Check to see if the username AND password MATCHES the username AND password in the DB else { $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user' and password = '$pass'") or die("Can not query DB."); $count = mysqli_num_rows($query); if($count == 1){ //YES WE FOUND A MATCH! $_SESSION['username'] = $user; //Create a session for the user! header ("location: members.php"); } else{ echo "Username and Password DO NOT MATCH! TRY AGAIN!"; } } } ?> <html> <head> <title>Test</title> </head> <body style="background-color:#2E650D;"> <table style="margin-top:200px;"> <table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black"> <tr> <form name="register" method="post" action="login.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ccc"> <tr> <td colspan="3"><strong><center>Login </center></strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="user" type="text" id="user"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="pass" type="password" id="pass"></td> </tr> <tr> <td></td> <td></td> <td><input type="submit" name="submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> </table> </html> logout.php: <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. */ session_start(); require "config.php"; session_destroy(); echo "You have successfully logged out. <a href='login.php'> Click here </a> to login!"; ?> members.php: <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. 6) NOT NEEDED BUT IF YOU WANT THE ONLY USERS TO VIEW AN APPLICATION THEN THIS MIGHT BE USEFUL. */ //IF THE USER IS LOGGED IN THEN TELL THE USER, ELSE TELL THE USER TO LOG IN! session_start(); require "config.php"; //Check to see if the user is logged in. if(isset($_SESSION['username'])){ echo "Hello ".$_SESSION['username'].", you are logged in. <br /> This the member's page! Nothing here . <a href='logout.php'>Click Here </a>to log out."; } else{ echo "Please <a href='login.php'>Log In </a> to view the content on this page!"; } ?> That is all the coding. So why wont the login work? Quote Link to comment Share on other sites More sharing options...
manjunath_goku Posted April 14, 2014 Share Posted April 14, 2014 What is the problem? Does it throw any error? Or does it always tell you the username/password is incorrect? Quote Link to comment Share on other sites More sharing options...
ZappyLawns Posted April 14, 2014 Author Share Posted April 14, 2014 (edited) Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/zappylaw/public_html/login.php:2) in /home/zappylaw/public_html/login.php on line 13Warning: Cannot modify header information - headers already sent by (output started at /home/zappylaw/public_html/login.php:2) in /home/zappylaw/public_html/login.php on line 18 Login Username : Password : Warning: Cannot modify header information - headers already sent by (output started at /home/zappylaw/public_html/config.php:21) in /home/zappylaw/public_html/register.php on line 21 Edited April 14, 2014 by ZappyLawns Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2014 Share Posted April 14, 2014 Hi, the code you gave us above is not the code you actually use. If you read the error messages, it tells you exactly what is wrong: You have output in line 2 of login.php which prevents the session_start() in line 13 from sending HTTP headers. There must not be any output before session_start(). No whitespace, no HTML doctype, no BOM, absolutely nothing. The PHP tag must start at the very first byte of the file. Quote Link to comment Share on other sites More sharing options...
manjunath_goku Posted April 14, 2014 Share Posted April 14, 2014 There are few possible reason you are getting this warnings: 1. There is some space before your first <?php tag, sometimes, your editor can add some garbage value. Check it has not added anything like that. 2. Move you session_start(); to the top, exactly after <?php, sometimes a white space before the session_start(); could mean, you are printing something, before the session has started. 3. If the session has already started then do not call session_start() again. Like below, if(!isset($_SESSION)) session_start(); Any of the three could solve the problem. Let me know, if it does solve it. Quote Link to comment Share on other sites More sharing options...
ZappyLawns Posted April 14, 2014 Author Share Posted April 14, 2014 heres what it looks like now. One error is gone in login but theres still 1 more error and an error in register. <?php session_start(); require "config.php"; if(isset($_SESSION['username'])){ header("location: members.php"); //isset check to see if a variables has been 'set' } if(isset($_POST['submit'])) { //Variables from the table $user = $_POST['user']; $pass = $_POST['pass']; //Prevent MySQL Injections $user = stripslashes($user); $pass = stripslashes($pass); $user = mysqli_real_escape_string($con, $user); $pass = mysqli_real_escape_string($con, $pass); //Check to see if the user left any space empty! if($user == "" || $pass == "") { echo "Please fill in all the information!"; } //Check to see if the username AND password MATCHES the username AND password in the DB else { $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user' and password = '$pass'") or die("Can not query DB."); $count = mysqli_num_rows($query); if($count == 1){ //YES WE FOUND A MATCH! $_SESSION['username'] = $user; //Create a session for the user! header ("location: members.php"); } else{ echo "Username and Password DO NOT MATCH! TRY AGAIN!"; } } } ?> Warning: Cannot modify header information - headers already sent by (output started at /home/zappylaw/public_html/config.php:21) in /home/zappylaw/public_html/login.php on line 5 Warning: Cannot modify header information - headers already sent by (output started at /home/zappylaw/public_html/config.php:21) in /home/zappylaw/public_html/register.php on line 21 Quote Link to comment Share on other sites More sharing options...
Solution manjunath_goku Posted April 14, 2014 Solution Share Posted April 14, 2014 Allrite, try this - In config.php, remove the ?> tag from the bottom, save it. And then try running it and see if you still get the errors. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 14, 2014 Share Posted April 14, 2014 Read the error messages. I mean, really, this is standard English. You don't need a CS degree to understand what they're saying. Quote Link to comment Share on other sites More sharing options...
ZappyLawns Posted April 14, 2014 Author Share Posted April 14, 2014 Allrite, try this - In config.php, remove the ?> tag from the bottom, save it. And then try running it and see if you still get the errors. I did what you said it when i saved it i loaded the page and it switches the page over to members.php and says error page not found. Quote Link to comment Share on other sites More sharing options...
therocker Posted April 14, 2014 Share Posted April 14, 2014 Try putting this in front of your first <?php <?php ob_start(); ?> And this at the end of your entire code. <?php ob_flush(); ?> Quote Link to comment Share on other sites More sharing options...
ZappyLawns Posted April 14, 2014 Author Share Posted April 14, 2014 Error on Registration: Warning: Cannot modify header information - headers already sent by (output started at /home/zappylaw/public_html/config.php:19) in /home/zappylaw/public_html/register.php on line 21 Error on Login: Warning: Cannot modify header information - headers already sent by (output started at /home/zappylaw/public_html/config.php:19) in /home/zappylaw/public_html/login.php on line 17 Ok I am left with 2 errors and the codes are listed below config.php <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. 6) I DIDN'T MAKE THE VARIABLES FINAL ON PURPOSE! DON'T FLAME. */ //Information to connect to your MySQL Server AND DB $host = "localhost"; $username = "zappylaw_rob"; $password = "haha12"; $db = "zappylaw_login"; $con = mysqli_connect($host,$username,$password,$db) or die("Can not connect to Server."); ?> login.php <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. */ session_start(); require "config.php"; //Connection Script, include in every file! //Check to see if the user is logged in. if(isset($_SESSION['username'])){ header("location: members.php"); //isset check to see if a variables has been 'set' } if(isset($_POST['submit'])) { //Variables from the table $user = $_POST['user']; $pass = $_POST['pass']; //Prevent MySQL Injections $user = stripslashes($user); $pass = stripslashes($pass); $user = mysqli_real_escape_string($con, $user); $pass = mysqli_real_escape_string($con, $pass); //Check to see if the user left any space empty! if($user == "" || $pass == "") { echo "Please fill in all the information!"; } //Check to see if the username AND password MATCHES the username AND password in the DB else { $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user' and password = '$pass'") or die("Can not query DB."); $count = mysqli_num_rows($query); if($count == 1){ //YES WE FOUND A MATCH! $_SESSION['username'] = $user; //Create a session for the user! header ("location: members.php"); } else{ echo "Username and Password DO NOT MATCH! TRY AGAIN!"; } } } ?> <html> <head> <title>Test</title> </head> <body style="background-color:#2E650D;"> <table style="margin-top:200px;"> <table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black"> <tr> <form name="register" method="post" action="login.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ccc"> <tr> <td colspan="3"><strong><center>Login </center></strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="user" type="text" id="user"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="pass" type="password" id="pass"></td> </tr> <tr> <td></td> <td></td> <td><input type="submit" name="submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table> </table> </html> logout.php <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. */ session_start(); require "config.php"; session_destroy(); echo "You have successfully logged out. <a href='login.php'> Click here </a> to login!"; ?> members.php <?php session_start(); require "config.php"; //Check to see if the user is logged in. if(isset($_SESSION['username'])){ echo "Hello ".$_SESSION['username'].", you are logged in. <br /> This the member's page!<a href='logout.php'>Click Here </a>to log out."; } else{ echo "Please <a href='login.php'>Log In </a> to view the content on this page!"; } ?> <html> <head> <title>Zappy Lawns | Members Area | ZappyLawns</title> <link rel="stylesheet" media="screen" type="text/css" href="memberstyle.css" /> </head> <body style="background-color:#2E650D;"> <div id="ad"> <a href="http://secure.hostgator.com/~affiliat/cgi-bin/affiliates/clickthru.cgi?id=zappylawns-1"><img src="http://tracking.hostgator.com/img/Shared_Blue/728x90-animated.gif" border="0"></a> </div> <div id="page"> <div id="header"> <div id="logo"> <!-- type your logo and small slogan here --> <p>Zappy<span class="green">Lawns</span></p> <div id="slogan"><p>your lawn will be ready in a zap.</p></div> <!-- end logo --> </div> <div id="phone"> <p>732-818-9880</p> </div> <div id="nav"> <div id="nav-menu-left"></div> <div id="nav-menu"> <!-- start of navigation --> <ul> <li><a href="index.html">Home</a></li> <li><a href="rates.html">Rates/Services</a></li> <li><a href="terms.html">Terms</a></li> <li><a href="faq.html">FAQ</a></li> <li><a href="help.html">Help Me</a></li> <li><a href="contact.html" style="background-image: none;">Contact Us</a></li> </ul> <!-- end navigation --> </div> <div id="nav-menu-right"></div> </div> </body> </html> register.php <?php /* NOTES: 1) THIS IS A SIMPLE SCRIPT, SO I WILL USE IF/ELSE STATEMENTS. THE CODE IS VERY SIMPLE. IT MIGHT BE SLOPPY BUT IT'S SIMPLE. 2) IN THE ADVANCED TUTORIAL I WILL ADD CLASSES / METHODS! 3) I WILL NOT USE MD5 IN THIS TUTORIAL 4) YOU MUST KNOW BASIC MYSQL AND PHP, IF NOT THEN GOOGLE SOME TUTORIALS. 5) DO NOT SUGGEST ANY COMPLICATIONS. THIS IS FOR NEW PHP DEVELOPERS NOT ADVANCED DEVELOPERS. */ session_start(); //Must Start a session. require "config.php"; //Connection Script, include in every file! //Check to see if the user is logged in. //'isset' check to see if a variables has been 'set' if(isset($_SESSION['username'])){ header("location: members.php"); } //Check to see if the user click the button if(isset($_POST['submit'])) { //Variables from the table $user = $_POST['user']; $pass = $_POST['pass']; $rpass = $_POST['rpass']; //Prevent MySQL Injections $user = stripslashes($user); $pass = stripslashes($pass); $rpass = stripslashes($rpass); $user = mysqli_real_escape_string($con, $user); $pass = mysqli_real_escape_string($con, $pass); $rpass = mysqli_real_escape_string($con, $rpass); //Check to see if the user left any space empty! if($user == "" || $pass == "" || $rpass == "") { echo "Please fill in all the information!"; } else { //Check too see if the user's Passwords Matches! if($pass != $rpass) { echo "Passwords do not match! Try Again"; } //CHECK TO SEE IF THE USERNAME IS TAKEN, IF NOT THEN ADD USERNAME AND PASSWORD INTO THE DB else { //Query the DB $query = mysqli_query($con,"SELECT * FROM members WHERE username = '$user'") or die("Can not query the TABLE!"); //Count the number of rows. If a row exist, then the username exist! $row = mysqli_num_rows($query); if($row == 1) { echo "Sorry, but the username is already taken! Try again."; } //ADD THE USERNAME TO THE DB else { $add = mysqli_query($con,"INSERT INTO members (id, username, password) VALUES (null, '$user' , '$pass') ") or die("Can't Insert! "); echo "Successful! <a href='members.php'> Click Here </a> to log In."; } } } } ?> <html> <head> <title>Test</title> </head> <body style="background-color:#2E650D;"> <table style="margin-top:200px;"> <table width="300" align="center" cellpadding="0" cellspacing="1" border="1px solid black"> <tr> <form name="register" method="post" action="register.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#ccc"> <tr> <td colspan="3"><strong><center>Registration</center></strong></t d> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="user" type="text" id="user"></td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="pass" type="password" id="pass"></td> </tr> <tr> <td>Repeat Password</td> <td>:</td> <td><input name="rpass" type="password" id="rpass"></td> </tr> <tr> <td></td> <td></td> <td><input type="submit" name="submit" value="Register"></td> </tr> </table> </td> </form> </tr> </table> </table> </body> </html> Quote Link to comment Share on other sites More sharing options...
therocker Posted April 15, 2014 Share Posted April 15, 2014 What seems to be the problem? Quote Link to comment Share on other sites More sharing options...
ZappyLawns Posted April 15, 2014 Author Share Posted April 15, 2014 therocker? Howd you do that? It just gives me tons of errors! 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.