Click to See Complete Forum and Search --> : Login Cookies...


spykemitchell
09-21-2003, 09:31 AM
I'm not sure if this is done with PHP or not but here goes,

I want to create a screen that the first time someone enters my site it says something like, This site requires such and such

and then when the user clicks ok it takes them to a new page an places a cookie on their computer that means next time they visit it bypasses the This site requires screen and takes them straight to the next one...

I hope that made sense...

pyro
09-21-2003, 09:39 AM
Yep, just use setcookie() (http://us2.php.net/manual/en/function.setcookie.php) to set a cookie, and something like this to read if the cookie has been set and redirect using the header (http://us2.php.net/manual/en/function.header.php):

if (isset($_COOKIE['cookiename'])) {
header("Location:http://www.yourdomain.com/somepage.php");
}

spykemitchell
09-21-2003, 09:59 AM
So if i put

<?php

set cookie('visited')

?>

In my main.php document (assuming main.php is the page after checking your browser meets the specifications)

and then

<?php

if (isset($_COOKIE['visited'])) {
header("Location:main.php");
}

?>

in the specifications page so that when they open my site and they have the cookie installed they are redirected to main.php...

I only recently started learning PHP so i need a bit of guidence...

Spyke

pyro
09-21-2003, 10:03 AM
A couple of things. It is setcookie() not set cookie(), so you'll have to remove the space. Also, if you don't set a time that the cookie expires, it will expire when the browser is closed. You'll probably want to set an expiration time...

Also, the header location takes on a full url, not a relative one, so it should be:

header("Location:http://www.yourdomain.com/main.php");

spykemitchell
09-21-2003, 10:10 AM
This is my re-edited code...

<?php

setcookie('visited', time()+3600)

?>

In my main.php document (assuming main.php is the page after checking your browser meets the specifications)

and then

<?php

if (isset($_COOKIE['visited'])) {
header("Location:http://www.mysite.co.uk/main.php");
}

?>

This will make the cookie expire in one hour, right?

How exactly does the time function work?

spykemitchell
09-21-2003, 10:33 AM
I get the following error message in the php document where the

<?php

setcookie('visited', time()+3600)

?>

script is held...


Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Abyss Web Server\htdocs\spykes\default\index1.php:5) in C:\Program Files\Abyss Web Server\htdocs\spykes\default\index1.php on line 20

Do you know what is wrong with it?

pyro
09-21-2003, 12:47 PM
All of that code (the setcookie() and the header()) needs to come before any content is sent to the browser, including you <html> or a DOCTYPE tag...

spykemitchell
09-21-2003, 01:55 PM
It now doesn't show the error but it doesn't place thecookie either...

pyro
09-21-2003, 03:32 PM
You have the paramaters wrong. If you look on the link I gave you to php.net, you'll see that it should be cookiename, cookievalue and then the time.

<?php
setcookie('visited', true, time()+3600); #time()+3600 equal one hr.
?>

spykemitchell
09-22-2003, 11:55 AM
Ahhhhh.......

I see....

Now it works. I'm going to read over that link you gave me to learn about it again.

Spyke.

P.s. Yes the "It's Friday" post did fail...