Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Page isn't storing cookies
    text
    copied!<p>I am new to php and I am making a basic login script. All I want to do is log in and have the cookie for my user_id stored. It works on all of my other pages, except my index page which is one directory up.</p> <p>So on my index page, I have this if statement: </p> <pre><code>&lt;?php if (!isset($_COOKIE['user_id'])) { sign_in(); } else { echo "You're already logged in!"; } ?&gt; </code></pre> <p>No matter what I do, the sign_(); function always shows. </p> <p>But here's the kicker: On my login script, the whole thing goes through as if I successfully logged in. I send it back to this page using: </p> <pre><code>header("Location: ../index.php"); </code></pre> <p>(It is up one directory) However, when I make it link to a page in the same directory, it registers the cookie and everything is alright.</p> <pre><code>header("Location: show_user.php"); </code></pre> <p>If you want a hands on view, you can go to <a href="http://patti-bee2.dcccd.edu/coleman/wonder%20penguin/php/signup.php" rel="nofollow">http://patti-bee2.dcccd.edu/coleman/wonder%20penguin/php/signup.php</a> to make your account. And <a href="http://patti-bee2.dcccd.edu/coleman/wonder%20penguin/php/show_user.php" rel="nofollow">http://patti-bee2.dcccd.edu/coleman/wonder%20penguin/php/show_user.php</a> to view it. And notice how the index page doesn't register the cookie. </p> <p>How I tried to set the cookie:</p> <pre><code>if (isset($_POST['usernamelogin'])) { $user_login = $_REQUEST['usernamelogin']; $pass_login = $_REQUEST['passwordlogin']; $pass_login = trim(crypt($pass_login, $user_login)); $login_query = sprintf("SELECT username, user_id FROM user WHERE username = '%s' and password = '%s';", mysql_real_escape_string($user_login), mysql_real_escape_string($pass_login)); $loginresult = mysql_query($login_query, $dbConn); echo $login_query; if (mysql_num_rows($loginresult) == 1) { $userinfo = mysql_fetch_array($loginresult); $username = $userinfo['username']; $userid = $userinfo['user_id']; setcookie('username', $username); setcookie('user_id', $userid); header("Location: show_user.php"); exit(); } else { echo "Couldn't find your account!"; } } </code></pre> <p>Please excuse my unrefined page and amateur mistakes. I have a lot to learn.</p> <p>Any ideas?</p> <p>Thank you for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload