MrFish Posted September 12, 2009 Share Posted September 12, 2009 I'm trying to use ajax to log in users, but I get this error message. Warning: Cannot modify header information - headers already sent by (output started at /home/mandala/public_html/includes/loginhandler.php:20) in /home/mandala/public_html/includes/loginhandler.php on line 22Warning: Cannot modify header information - headers already sent by (output started at /home/mandala/public_html/includes/loginhandler.php:20) in /home/mandala/public_html/includes/loginhandler.php on line 23The Login handler code is.<? session_start(); $con = mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("mandala_maindb"); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $query="SELECT * FROM users WHERE username ='$username'"; $run = mysql_query($query); $results = mysql_affected_rows(); if($results!=1){ //The username doesn't exist echo 'username false'; return false; } $query="SELECT * FROM users WHERE username='$username' AND password='$password'"; $run = mysql_query($query); $results = mysql_affected_rows(); if($results==1){ //login success echo 'login true'; $_SESSION["username"] = $username; setCookie("username", $username, time()+60*60*24*30,'/'); setCookie("password", $password, time()+60*60*24*30,'/'); } else { echo 'login false'; return false; }?> Link to comment Share on other sites More sharing options...
Guest FirefoxRocks Posted September 12, 2009 Share Posted September 12, 2009 http://w3schools.invisionzone.com/index.ph...ost&p=96607 Link to comment Share on other sites More sharing options...
Ingolme Posted September 12, 2009 Share Posted September 12, 2009 You can't set a cookie after writing anything to the page (using echo or putting HTML before the cookie) Link to comment Share on other sites More sharing options...
MrFish Posted September 13, 2009 Author Share Posted September 13, 2009 Yes but I thought with ajax there was a way to set a cookie. Link to comment Share on other sites More sharing options...
Ingolme Posted September 13, 2009 Share Posted September 13, 2009 If you're using AJAX, set the cookie with Javascript (for more details, check the W3Schools Javascript tutorial). You cannot set a cookie after outputting content. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 14, 2009 Share Posted September 14, 2009 Using ajax doesn't change the rules for PHP. Just echo "login true" after you set the cookie, not before. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.