0

Maybe im missing something but im not sure. All im simply trying to do is set cookies so i can retrieve their value later. Im trying to implement such a thing but to no avail....not sure why its not working since its not that difficult.

  1. i have a simple log in form you know, username/pass etc...nothing fancy.

  2. On successful login, i have something like this:

    //COOKIE DETAILS HERE

    setcookie('username4pc',$userName);
    setcookie('userpass4pc',$userPass);
    header("Location:testCookie.php");
    

And ive made sure this is going before the HTML tag and before anything is outputted to the page. So much so that, ive taken out the above from my program that im working on and put it on a blank html page to test, and it now looks like this...

after clicking on "login" on the indexpage, it goes to my new test page which only has this:

<?php
setcookie('username4pc',$userName);
setcookie('userpass4pc',$userPass);
header("Location:testCookie.php");
?>
<html></html>

and after the above runs, the header throws it to testCookie.php page which only has this

<?php
//test cookie
if(isset($_COOKIE['first_name'])){
 echo " cookies set";
} else {
 echo "cookie not set"; 
} 
?>
<html></html>

and no matter what i do, i always get the "cookie not set"

Any ideas as to why and whats going wrong?

Juergen
12.8k7 gold badges43 silver badges56 bronze badges
asked Jun 19, 2012 at 15:27
1
  • 2
    Your cookie is not named 'first_name', it's named 'username4pc' so that's why you can't retrieve it. Commented Jun 19, 2012 at 15:30

2 Answers 2

5

You're accessing the cookie wrong, it should be:

if(isset($_COOKIE['username4pc'])){
or
if(isset($_COOKIE['userpass4pc'])){

a simple var_dump($_COOKIE) will show you exactly what's in the cookie array.

answered Jun 19, 2012 at 15:29
Sign up to request clarification or add additional context in comments.

1 Comment

GOD!!! i feel ...soo pissed off i been on this ALL MORNING!!! after reading back im like wait....wtf...thats wrong lol.....thanks @Marc B...stupid moment i swear lol...thanks again
1

First thing I see is that you are setting and checking two different things.

What you are checking for:

isset($_COOKIE['first_name'])

What you are setting

setcookie('username4pc',$userName);
setcookie('userpass4pc',$userPass);
answered Jun 19, 2012 at 15:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.