pocobueno1388 Posted June 4, 2007 Share Posted June 4, 2007 I just made a login script and I made it so the user could check if they want to stay logged in forever. But when they check the box the cookie won't initiate. If they don't check the box, the session registers just fine. <?php if (isset($_POST['stay_logged'])){ setcookie("sid", $row['userID'], time()+360000); } else { $_SESSION['sid'] = $row['userID']; } ?> I made sure that $_POST['stay_logged'] was set, so thats not the problem. Any help with this is greatly appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/ Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 Is it maybe because "$row['userID']" holds a boolean value? My "userID" is 1. Here is what the Manuel says: Because setting a cookie with a value of FALSE will try to delete the cookie, you should not use boolean values. Instead, use 0 for FALSE and 1 for TRUE. Is there away around this IF this is what is causing the problem? Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267768 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 Are you programming on Localhost, if so setting cookies has to be done a certain way: see http://www.aeonity.com/frost/php-setcookie-localhost-apache gives a good explanation. Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267772 Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 Nope, I'm not programming on Localhost. By that you mean by using a program such as XAMPP or something, correct? I am hosting at hostgator, so no...it's not running from my computer. Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267796 Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 Found the problem, I had it redirecting after the cookie sent, so it redirected so fast I couldn't see the error. Warning: Cannot modify header information - headers already sent by (output started at /home/colin/public_html/config.php:26) in /home/colin/public_html/check_login.php on line 16 I complete understand what that error means, but there was no output started in my config file...the only code that is there is the database connection, absolutely no output. Why would I be getting this error? Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267801 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 whats on line 26 of the config file? Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267810 Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 EDIT: It's giving me the header error again all of the sudden...I didn't change anything 0_0 config.php <?php $dbh=mysql_connect("***","***","***") or die("...."); $db = mysql_select_db(***,$dbh)or die("----"); function clean($str){ $str = trim(mysql_real_escape_string($str)); return $str; } function error($msg){ if (!empty($msg)){ echo "<center><font color='red'><b>Error(s)</b></font><br>"; foreach ($msg as $key => $val){ echo "<li><b>$val</b></li>"; } echo '<p>'; } } function msg($str){ echo "<b>Success!</b><br>"; echo "$str<p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267813 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 www.php.net/setcookie look at the path and domain options, try setting those and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267820 Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 Still getting the header error from line 27 of the config file and there are only 25 lines in the file... Here is how I did the cookie though: setcookie("sid", $row['userID'], time()+360000, "/public_html/", ".colinscode.com"); Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267829 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 Post the full config file. Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267865 Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 That was the full config file....here are ALL the files relating to this: CONFIG.PHP <?php $dbh=mysql_connect("***","***","***") or die("...."); $db = mysql_select_db(***,$dbh)or die("----"); function clean($str){ $str = trim(mysql_real_escape_string($str)); return $str; } function error($msg){ if (!empty($msg)){ echo "<center><font color='red'><b>Error(s)</b></font><br>"; foreach ($msg as $key => $val){ echo "<li><b>$val</b></li>"; } echo '<p>'; } } function msg($str){ echo "<b>Success!</b><br>"; echo "$str<p>"; } ?> LOGIN FORM [index.php] <form action="check_login.php" method="POST"> <table width="300" cellpadding=3> <td colspan=2> <h1>Login</h1></td> <tr> <td class="medium"> <b>Login:</b></td> <td class="light"> <input type="text" name="username"></td> <tr> <td class="medium"> <b>Password:</b></td> <td class="light"> <input type="password" name="password"></td> <tr> <td colspan=2 class="medium"> <input type="checkbox" name="stay_logged"> <i>Stay logged in forever?</i></td> <tr> <td colspan=2> <input type="submit" name="go" value="Login"></td> </table> </form> CHECK_LOGIN.PHP [The page it goes to when you try logging in] <?php session_start(); //This will cause a header already sent error...but I don't know a way around //that when trying to set a cookie. include 'config.php'; $login = $_POST['username']; $pass = md5($_POST['password']); //see if they got the correct info $chkInfo = mysql_query("SELECT userID FROM users WHERE login='$login' && password='$pass'"); if (mysql_num_rows($chkInfo) > 0){ //check if they want to stay logged in $row = mysql_fetch_assoc($chkInfo); if (isset($_POST['stay_logged'])){ setcookie("sid", $row['userID'], time()+360000, "/public_html/", ".colinscode.com"); } else { $_SESSION['sid'] = $row['userID']; } echo '<script language="javaScript"> window.location = "news.php?login=yes" </script>'; } else { $error[] = "Wrong login/password Combination."; error($error); echo '<a href="index.php">Try Again</a>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267902 Share on other sites More sharing options...
per1os Posted June 4, 2007 Share Posted June 4, 2007 Make sure in the config file, there is not an extra space after the ?> or before <?php tags. If there is that can be read as being ouput to the screen. Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267914 Share on other sites More sharing options...
pocobueno1388 Posted June 4, 2007 Author Share Posted June 4, 2007 Make sure in the config file, there is not an extra space after the ?> or before <?php tags. If there is that can be read as being ouput to the screen. That was it Frost, I had 2 extra lines. I wasn't aware that there could be output without a command. Thank you very very much =] Quote Link to comment https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/#findComment-267917 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.