Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/54162-solved-cookie-wont-set/
Share on other sites

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?

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?

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>";
}

?>

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"); 

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>';
      
   }    

?>

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 =]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.