need codes to set cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

need codes to set cookies

Post by sean82 »

hi
i look for a script that makes a link on an introducing page and by clicking the link cookies are set for this user and when he visits this page again he will be redirected to other url,
so he doesnt need always to visit this page
thx for help
Archy
Forum Contributor
Posts: 129
Joined: Fri Jun 18, 2004 2:25 pm
Location: USA

Post by Archy »

You will need to set a cookie:

Code: Select all

setcookie('cookie_name', 'Cookie value', time()+86400);
Think this is a day in time.

Then just make a redirection script thing:

Code: Select all

<?PHP
ob_start();

if($_GET['$Cookie_name'] != ""){
     header("Location: URL.com"); exit();
}

...
...
?>
Tha should be a start.
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

Post by sean82 »

hi
thx i tried this but unfortunately it doesnt work

Code: Select all

<?
setcookie('nomoremovie', 'Cookie value', time()+86400);
?>
<?PHP
ob_start();
if($_GET['$nomoremovie'] != ""){   
  header("Location: http://www.mysite.com/index.php?crash=start");exit();
  }
  ...
  ...
  ?>
the setcookie was on the very 1 line
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

Code: Select all

<?php


$cookie_expire = time() + 86400; // 1 day

if (isSet($_GET['please_set_my_cookie'])) {
    setcookie('nomoremovie', 'true', $cookie_expire);
    header("Location: http://www.mysite.com/index.php?crash=start");
    exit;
}

if (isSet($_COOKIE['nomoremovie'])) {
    header("Location: http://www.mysite.com/index.php?crash=start");
    exit;
}

?>

then make a link which directs to this script like
<a href="script.php?please_set_my_cookie">remember me</a>

is that what you mean?
User avatar
sean82
Forum Newbie
Posts: 8
Joined: Fri Apr 16, 2004 5:33 am

Post by sean82 »

exactly what i need but it gives this error

Warning: Cannot modify header information - headers already sent by (output started at /home/www/web../html/nomoremovie.php:2) in /home/www/web../html/nomoremovie.php on line 5

i made the codes in a nomoremovie.php file and a link this way
<a href="nomoremovie.php?please_set_my_cookie">remember me</a>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Post Reply