doddsey_65 Posted February 18, 2011 Share Posted February 18, 2011 when a user logs in the cookie isnt being set. am i doing it wrong? if(empty($error)) { $query = $link->query("SELECT * FROM ".TBL_PREFIX."users WHERE u_username = '$username' AND u_password = '".asf_hash($password)."'") or die(print_link_error()); $row = $query->fetchAll(); $num_rows = $query->rowCount(); if($num_rows == 1) { if($row[0]['u_confirmed'] == 1) { setcookie('uid', $row[0]['u_uid'], time() + $session_length); // this cookie isnt being set echo 1; } else { $error = 'You Need To Activate Your Account'; } } else { if(!$error) { echo $lang->incorrect_login_details; } } } im doing a print_r on all cookies and it doesnt appear in the list. $session_length is set at 99999999 Quote Link to comment https://forums.phpfreaks.com/topic/228079-setcookie-not-working/ Share on other sites More sharing options...
ronverdonk Posted February 18, 2011 Share Posted February 18, 2011 PHP manual: "setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace. When the cookie is not set, a FALSE is returned." So I suggest that you check the result of setcookie() to check, as in: if (setcookie('uid', $row[0]['u_uid'], time() + $session_length) === false) {// this cookie isnt being set echo 'Setcookie failed'; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/228079-setcookie-not-working/#findComment-1176136 Share on other sites More sharing options...
doddsey_65 Posted February 18, 2011 Author Share Posted February 18, 2011 i have tried: if (setcookie('uid', $row[0]['u_uid'], time() + $session_length) == false) { echo 'Setcookie failed'; } else { echo 'success'; } and it echos success, but no cookie is added. Could this be something to do with WAMP server? Quote Link to comment https://forums.phpfreaks.com/topic/228079-setcookie-not-working/#findComment-1176138 Share on other sites More sharing options...
BlueSkyIS Posted February 18, 2011 Share Posted February 18, 2011 if you set a cookie in the script, that cookie will not be visible until the next page load. therefore, if you check for the cookie right after setting it, you won't see it because it isn't there. Quote Link to comment https://forums.phpfreaks.com/topic/228079-setcookie-not-working/#findComment-1176260 Share on other sites More sharing options...
doddsey_65 Posted February 19, 2011 Author Share Posted February 19, 2011 when the processing of the login is complete it passes a value to a javascript file which then redirects to the page they were last on, or the index page if no redirect session exists. Im trying to upload the files to the server to see if this is a localhost thing but filezilla always breaks down so its taking a while lol. also does anyone know why i might have phpbb3 cookies on my localhost site? i do have a setup of phpbb in another folder but not this one. Quote Link to comment https://forums.phpfreaks.com/topic/228079-setcookie-not-working/#findComment-1176617 Share on other sites More sharing options...
doddsey_65 Posted February 19, 2011 Author Share Posted February 19, 2011 ok here is the whole file, i hope someone can help. when it is successful it echos 1 back to the javascript file which redirects to the page they were on or redirects back to index.php if there isnt a redirect session. <?php ob_start(); /* ASF A Simple Forum * Version 0.1.0 * Page Created 21/01/2011 */ // can page be accessed directly? define('ACCESS', TRUE); // start sessions session_start(); // get the functions file include(dirname(dirname(__FILE__)).'/functions/functions.php'); include(dirname(dirname(__FILE__)).'/includes/initialize.php'); if(isset($_POST['user_name']) && isset($_POST['password'])) { $username = $_POST['user_name']; $password = $_POST['password']; $session_length = $_POST['session_length']; if(empty($session_length) &&!$error) { $error = 'Invalid Session Length'; } if(empty($username)) { if(!$error) { $error = 'Please Enter A Username'; } } if(empty($password)) { if(!$error) { $error = 'Please Enter A Password'; } } if(empty($error)) { $query = $link->query("SELECT * FROM ".TBL_PREFIX."users WHERE u_username = '$username' AND u_password = '".asf_hash($password)."' LIMIT 1") or die(print_link_error()); $row = $query->fetchAll(); $num_rows = $query->rowCount(); if($num_rows == 1) { if($row[0]['u_confirmed'] == 1 && $row[0]['u_banned'] == 0) { setcookie("uid", "1", time()+3600); if (setcookie('uid', "1", time()+3600) === false) { echo 'Failed To Set Cookie'; } else { echo 1; } } elseif($row[0]['u_banned'] == 1) { $error = 'You Have Been Banned From This Forum'; } else { $error = 'You Need To Activate Your Account'; } } else { if(!$error) { echo $lang->incorrect_login_details; } } } else { echo $error; } } ob_flush(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228079-setcookie-not-working/#findComment-1176624 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.