0

Trying to learn cookies in PHP to me everything seems fine but it doesnt even set the cookie
here is the code

<?php
 if (isset($_COOKIE['user'])) {
 header('Location: simple.php'); 
 } else {
 if (isset($_POST['submit'])) {
 if (isset($_POST['username']) && isset($_POST['password'])) {
 if (isset($_POST['checkbox'])) {
 $expire = time() + 60 * 60;
 setcookie('user', $_POST['username'], $expire);
 } else {
 header('Location: simple.php');
 }
 } else {
 echo 'Please Enter Your Information !';
 }
 } else {
 echo 'Please Submit';
 }
 }
?>

Edit

tried

<?php 
 setcookie("testcookie","testvalue",0); 
 echo $_COOKIE['testcookie'];
?>

Result is

Notice: Undefined index: testcookie in /var/www/php-practice/cookies/test.php on line 1

and it sets the cookie testcookie in browser with the value testvalue
Feel there is some error in $_POST['submit'] because

if (isset($_POST['submit'])) {
 //everything else
} else {
 echo 'Submit Button Problem !';
}

it prints the Submit Button Problem ! here is the HTML of the submit button

<input type="submit" value="Submit" name="submit" />

Looked at this question and tried it but still nothing

I tried everything I could but it doesnt work
Help !

asked Dec 4, 2013 at 11:35
16
  • you should use sessions, in most use-cases. in this case, I could simply create a cookie on my side and I'd be "authenticated". Commented Dec 4, 2013 at 11:36
  • What happens? Did you get redirected to simple.php or is there a message or something else? The setcookie() function looks ok. Commented Dec 4, 2013 at 11:38
  • @Prisoner just trying cookies for learning and its not working Commented Dec 4, 2013 at 11:38
  • Make sure to remove any whitespace from the beginning of your php file. Commented Dec 4, 2013 at 11:38
  • @MarcelBalzer it doesnt redirects to the simple.php Commented Dec 4, 2013 at 11:39

2 Answers 2

1

Note you can't set and display cookies on same page at same time. If you set and redirect to another page or reload the page, it will show the cookie value.

<?php 
 setcookie("testcookie","testvalue",0); //this will work
 echo $_COOKIE['testcookie']; // won't work unless reloaded
?>

For your initial script. Ensure that $_POST['checkbox'] exists, is checkbox the name of your html form checkbox input?

//Your HTML form should include this
<input type="checkbox" value="1" name="checkbox" /> 
 if (isset($_POST['checkbox'])) {
 $expire = time() + 60 * 60;
 setcookie('user', $_POST['username'], $expire);
 } else {
 header('Location: simple.php');
 }

Edit your HTML Form to include method="post"

<form method="post">
 <input type="text" name="username" />
 <input type="password" name="password" />
 <input type="checkbox" value="1" name="checkbox" /> 
 <input type="submit" value="Submit" name="submit" />
</form>
answered Dec 4, 2013 at 12:40
Sign up to request clarification or add additional context in comments.

2 Comments

it doesnt go inside the if (isset($_POST['submit'])) {} in html form i have submit is like that <input type="submit" value="Submit" name="submit" />
ensure your form has method="post" check my answer for modification
0

The $_COOKIE is only available after the site was loaded.

answered Dec 4, 2013 at 12:40

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.