Hi! I almost finished the login system for my Rickrolla website. But, I keep having problems with the Cookies. This error keeps occuring Here's the code for login.php <?php require "header.php"; ?> <body> <?php function login($errors=''){ if($errors){ print 'Correct the following errors...'; print '<li><ul>'; print implode('</li><li>',$errors); print '</li></ul>'; } print<<<_HTML_ <form method="POST" action="$_SERVER[PHP_SELF]"> Your Name: <input type="text" name="name"> <br> Your Password: <input type="password" name="password"> <br> Done!: <input type="submit" value="done"> <input type="hidden" name="_submit_check" value="1"> </form> _HTML_; } function retrieve(){ $errors= array(); mysql_connect("localhost", "admin", "password"); mysql_select_db("booo_db"); $user_name = $_POST['name']; $password =$_POST['password']; $query = "SELECT user_password FROM users WHERE user_name='{$user_name}'"; $result=mysql_query($query); $row=mysql_fetch_assoc($result); if(!$password=$row['password']){ $errors[]="Wrong username or password"; } } if($_POST['_submit_check']){ if($form_errors=retrieve()){ login($form_errors); }else{ doit(); } } if(!$_POST['_submit_check']){ login(); } ?> <?php require "footer.php"; ?> PHP: Here's the code for header.php <?php function doit(){ setcookie($row['user_name']); print "You're login Mr.".$_COOKIE['user_name']; } ?> <html> <head> <title>Rickroller.com</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <div id="Header"> <div id="Nav" align="right"> <a href="register.php">Register Here!</a>/ <?php if(empty($me)){ print '<a href="login.php">Login Here!</a>/'; }else{ print '<a href="user.php?user='.$me.'">My Homepage </a>/'; } ?> </div> </div> PHP:
the main issue that causes this error is whit space at the top and bottom of php files.... remeber to remove and space before the first and after the last php tags
Do u notice you actually output something before u call the doit() function. functions like setcookie() , and header() wouldn't allow any output before it (including blank spaces). replace your header.php (your most top file) with this to your header.php <?php ob_start(); function doit(){ setcookie($row['user_name']); print "You're login Mr.".$_COOKIE['user_name']; } ?> PHP: