tags:

views:

43

answers:

1

Hello, I use the following piece of code in an include file. Because it it used in two instances within my code, I wanted to separate it into another include file and use with require_once() where it is needed. However, I noticed that if I do that, the cookies won't set. Everything else seems to work though. Is this a bug or this just can't be done this way.

I have been learning PHP only for two weeks so please take it easy on me.

Thank you!

if(mysqli_num_rows($checklogin) == 1)  
{  
    // set variables  
    $row = mysqli_fetch_array($checklogin);  
    $email = $row['Email'];  

    // create login sessions
    $_SESSION['UserName'] = $username;
    $_SESSION['Email'] = $email;
    $_SESSION['LoggedIn'] = 1;

    $cbxRememberMe = $_POST['cbxRememberMe'];

    // if remember me is checked
    if(isset($cbxRememberMe) && $cbxRememberMe == '1')
    {
     $row = mysqli_fetch_array($checklogin);

     // create cookies for autologin
     $expire = time() + AUTO_LOGIN_DURATION;
     $cookie_un = sha1(sha1($row['UserName']));
     $cookie_pass = sha1(sha1($row['Password']));

     setcookie('user', $cookie_un, $expire);
     setcookie('pass', $cookie_pass, $expire);
    }

    // get user's IP address
    $lastloginip = $_SERVER['REMOTE_ADDR'];

    // DB QUERY: update database activity
    // ------------------------------------------------------------------
    $updateactivity = mysqli_query($conn,"UPDATE users SET LastLoginDate = NOW(), LastActivityDate = NOW(), LastLoginIP = '$lastloginip' WHERE UserName = '$username'") 
    or die($updateactivity_error);
    // ------------------------------------------------------------------

    // redirect back to login to refresh
    header('Location: login.php');
}
A: 

A require()/include()'d file will execute exactly the same as if its contents had been embedded in the file doing the require/include. A cookie header looks exactly the same whether it's directly in a file, or done via an inclue.

I'd look at whether you've actually done a mysqli query before the require once line, since you've wrapped the entire include with that if (mysqli_num_rows(... business. Perhaps you should move the query definition/execution business into the include file as well.

Marc B