Detecting cookie snippet...

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
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Detecting cookie snippet...

Post by theda »

I have yet another question...

I posted a thread earlier about how to use cookies to create a skinning system, and got all the help I needed here, and it works just fine. Now onto my next task: I'm wondering if there is a way to detect if a cookie is set in a persons PC, and if not, have the page automatically redirect them to a page that would allow them to set a cookie to view the page correctly.

My idea was

Code: Select all

if (cookie is set) &#123; <my sites script here> &#125; else &#123; <redirection code> &#125;
What I don't know is how to check for a cookie doing the way I want to do it.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

Code: Select all

if(isset($_COOKIE&#1111;'whatever']))&#123;
$redirect = 0;
&#125;else&#123;
$redirect = 1;
&#125;
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Now when you have redirect = 0... and then redirect = 1... How is that actually going to redirect?

I've seen the "header("Location: ADDRESS");" thing... Im wondering if that'd work instead of redirect = 1/0.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do the redirect in the code block appropriate to the action you wish to take.

I believe the 'else' is where you want it.
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

What do you mean by that?

This is what I did:

Code: Select all

//Cookie check
if(isset($_COOKIE&#1111;'language']))&#123;
header("location: blah");
&#125;else&#123;
include($ver.".content.php");
&#125;
and all I get is "header error, already loaded..."

edit: the reason the header is already sent is because in my set.php page it uses headers to redirect.

edit 2: I'll look through my script to see if I can move things around to remove this error.

edit 3: I moved things around and it doesn't cause the error anymore, but instead of redirecting if it doesnt detect the cookie, it just... loads everything I told it not to load unless there is a cookie...anyway.

last edit: I moved things around and figured out what the problem is. I got everything working right. Thanks for the help.

PHENOM | PLEASE USE

Code: Select all

TAGS WHILE

Code: Select all

TAGS ARE OFFLINE[/color]
Post Reply