I found this tutorial on login username and password: <?php include("dbconnection.php"); /* Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); */ mysql_select_db($dbname) or die(mysql_error()); //Checks if there is a login cookie if(isset($_COOKIE['ID_my_site'])) //if there is, it logs you in and directes you to the members page { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $sql = "SELECT * FROM users WHERE username = '$username'"; $check = mysql_query($sql)or die(mysql_error()); while($info = mysql_fetch_array( $check )){ if ($pass != $info['password']){ }else{ header("Location: members.php"); } } } //if the login form is submitted if (isset($_POST['submit'])) { // if form has been submitted // makes sure they filled it in if(!$_POST['username'] | !$_POST['pass']){ //echo $sql; die('You did not fill in a required field.'); } // checks it against the database if (!get_magic_quotes_gpc()) { $_POST['email'] = addslashes($_POST['email']); } $check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error()); //Gives error if user dosen't exist $check2 = mysql_num_rows($check); if ($check2 == 0) { die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>'); } while($info = mysql_fetch_array( $check )){ $_POST['pass'] = stripslashes($_POST['pass']); $info['password'] = stripslashes($info['password']); $_POST['pass'] = md5($_POST['pass']); //gives error if the password is wrong if ($_POST['pass'] != $info['password']){ die('Incorrect password, please try again.'); }else{ // if login is ok then we add a cookie $_POST['username'] = stripslashes($_POST['username']); $hour = time() + 3600; setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); //then redirect them to the members area header("Location: members.php"); } } }else{ // if they are not logged in ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <table border="0"> <tr> <td colspan=2 style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;"> <h1>Login</h1> </td> </tr> <tr> <td style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;"> <label for="navbar_username"> User Name: </label> </td> <td style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;"> <input type="text" name="vb_login_username" id="username" style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" size="10" value="User Name" onblur="if (this.value == '') this.value = 'User Name';" onfocus="if (this.value == 'User Name') this.value = '';" /> </td> <td style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" nowrap="nowrap"> <label for="cb_cookieuser_navbar"> <input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" /> Remember Me? </label> </td> </tr> <tr> <td style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;"> <label for="navbar_password">Password:</label> </td> <td> <input type="password" style="font: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" <?php //name="pass"?> size="10" name="vb_login_username" id="pass" /> </td> <td> <input type="submit" style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" name="submit" value="Login" /> </td> </tr> <tr> <td style="font: 11px verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;"> <a href="registration.php">Register</a> </td> </tr> </table> </form> <?php } ?> PHP: The database was created successfully and the registration was made successfully. But it doesn't allow me to log in. Instead it says: Am I missing something?
<input type="password" style="font: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" <?php //name="pass"?> size="10" name="vb_login_username" id="pass" /> PHP: Change that ^ line to this line <input type="password" style="font: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;" size="10" name="pass" id="pass" /> PHP:
Oh that's right. The name attributes. Now I got that fixed and it says setcookie(ID_my_site, $_POST['username'], $hour);//line 58 setcookie(Key_my_site, $_POST['pass'], $hour);//line 59 //then redirect them to the members area header("Location: members.php");//line 62 PHP:
You cannot output anything to browser before Header function, so check that there is no echo, print etc...
setcookie(ID_my_site, $_POST['username'], $hour); setcookie(Key_my_site, $_POST['pass'], $hour); change that line ^ to this line setcookie('ID_my_site', $_POST['username'], $hour); setcookie('Key_my_site', $_POST['pass'], $hour); PHP:
When you say output you mean: echo or print or the whatever is printed right after ?> or before <? right? Because I've used this file as an include in the index file. Could that be the reason why it's not working? <!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></title> </head> <body> <?php /*this is the file within which the redirection occurs*/ include("login.php"); ?> </body> </html> PHP: Is there any way to get around this problem?