Php Redirecting Using Cookies

Status
Not open for further replies.

Hatter Madigan

Solid State Member
Messages
9
Here's what I wrote on another forum:

I'm a beginner at php (having started last week dry.gif ). I would like a script that would set a cookie when they click on a link so that they go to that page every time they visit the beginning page.

For example:

#1 : Start page w/script
#2 : Url in link that was clicked

So that when the person visits #1 again, they get automatically redirected to #2.

A little confusing..

So?
 
[code snipped to save space]

Hope that has helped ;)

Cheers,

~ Tkey
 
Two links, I don't understand what you meen.
The script above will set a cookie on the visitors first vist to the page. Then when the visitor comes again it will check for the cookie, if it exists will direct them to the page you specify in the code.

Is this what you are looking for?

Cheers,

~ Tkey
 
Well, it's language select page, and you need to select between languages. When you click one, you go to one of the languages home. When you go back to language select page, you get redirected to the language you selected before. There are two languages.
 
PHP:
<?php

$lang = $_GET['lang'];
$lang_cookie = $_COOKIE['language']; 

if (!empty ($lang_cookie)) {
	if ($lang_cookie == 'english' || $lang_cookie == 'spanish') {
		header ('location: '.$lang_cookie.'.php');
	} else {
		setcookie ('langauge','',time()-60);
	}
} else {
	if (!empty ($lang)) {
		if ($lang == 'english' || $lang == 'spanish') {
			setcookie ('language',$lang);

			header ('location: '.$lang.'.php');
		} else {
			header ('location: page1.php');
		}
	} else {
		echo '<a href="page1.php?lang=english">English</a><br />';
		echo '<a href="page1.php?lang=spanish">Spanish</a><br />';
	}
}

?>

This will be your first page. When you first go without a cookie, you'll see two links. I dunno what languages you want so I picked English and Spanish. After they click a link, a cookie called "language" will be set with a value of either "english" or "spanish", and they will be redirected to the page.

The next time they view the first page, an if statement checks for the existence of the cookie, and then whether the cookie value equals either "english" or "spanish". If these conditions are met, it redirects to the appropriate page.

I didn't test it but it should work.
 
Status
Not open for further replies.
Back
Top Bottom