Jump to content

Making cookies


Bounty

Recommended Posts

Ok here are my 3 php pages:

 

set.php

<?php
$Month = 2592000 + time(); 
$value = '1';
setcookie("cs_hpage", $value, $Month);
?>

 

get.php

<?php
  if (isset($_COOKIE["cs_hpage"])) {
    $cookie = $_COOKIE["cs_hpage"];
    print("Received a cookie: ".$cookie."\n");
  } else {
    print("Fail!.\n");
  }
?>

 

clear.php

<?php 
setcookie ("cs_hpage", "", time()-60000); 
?> 

 

It all works like charm but i dont know how to set

if $cookie == 1

echo "Cookie here!"

and if other then 1

echo "Fail!"

 

All i managed to do is as you can see to echo the cookie :/

 

And question no.2:

How can i set a cookie through a button?? (Like when pressed set cookie to value "1")

 

Thanks..

Link to comment
Share on other sites

It all works like charm but i dont know how to set

if $cookie == 1

echo "Cookie here!"

and if other then 1

echo "Fail!"

 

All i managed to do is as you can see to echo the cookie :/

You'd use another if statement,

get.php

<?php
  if (isset($_COOKIE["cs_hpage"])) {
    $cookie = $_COOKIE["cs_hpage"];
    if($cookie == '1') {
      print("Received a cookie: ".$cookie."\n");
    } else {
      print("Fail");
    }
  } else {
    print("Fail!.\n");
  }
?>

 

Or better yet merge the two together

<?php
  if (isset($_COOKIE["cs_hpage"]) && $_COOKIE["cs_hpage"] == '1') {
    $cookie = $_COOKIE["cs_hpage"];
    print("Received a cookie: ".$cookie."\n");
  } else {
    print("Fail!.\n");
  }
?>

 

And question no.2:

How can i set a cookie through a button?? (Like when pressed set cookie to value "1")

Like so

<?php

if(isset($_GET['submit'])) {
     setcookie('button_cookie', '2', time()+3600);
}

?>

<form action="" method="post">
<input type="submit" name="submit" value="Create Cookie" />
</form>

Link to comment
Share on other sites

<?php

if(isset($_GET['setcookie']) && $_GET['setcookie'] == '1') {
     setcookie('cookie_name', 'cookie_value', time()+3600);
}

?>

<h1>Link</h1>
<a href="?setcookie=1">Set Cookie</a>


<h1>Image</h1>
<a href="?setcookie=1"><img src="image.jpg" title="Set Cookie" /></a>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.