Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Php Redirecting Using Cookies
Closed Thread
Old 07-18-2008, 12:16 PM   #1 (permalink)
 
Newb Techie

Join Date: Jul 2008

Posts: 9

Hatter Madigan is on a distinguished road

Default Php Redirecting Using Cookies

Here's what I wrote on another forum:

Quote:
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?
Hatter Madigan is offline  
Old 07-18-2008, 12:27 PM   #2 (permalink)
Yek
 
T.F's Resident Cool Guy...

Join Date: Aug 2006

Posts: 1,625

Yek is on a distinguished road

Send a message via AIM to Yek Send a message via MSN to Yek Send a message via Yahoo to Yek Send a message via Skype™ to Yek
Default Re: Php Redirecting Using Cookies

[code snipped to save space]

Hope that has helped

Cheers,

~ Tkey

Last edited by Yek; 07-18-2008 at 04:05 PM.
Yek is offline  
Old 07-18-2008, 12:31 PM   #3 (permalink)
 
Newb Techie

Join Date: Jul 2008

Posts: 9

Hatter Madigan is on a distinguished road

Default Re: Php Redirecting Using Cookies

But you see, I have two links, not one :freak:

How would I do that?
Hatter Madigan is offline  
Old 07-18-2008, 12:40 PM   #4 (permalink)
Yek
 
T.F's Resident Cool Guy...

Join Date: Aug 2006

Posts: 1,625

Yek is on a distinguished road

Send a message via AIM to Yek Send a message via MSN to Yek Send a message via Yahoo to Yek Send a message via Skype™ to Yek
Default Re: Php Redirecting Using Cookies

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
Yek is offline  
Old 07-18-2008, 12:50 PM   #5 (permalink)
 
Newb Techie

Join Date: Jul 2008

Posts: 9

Hatter Madigan is on a distinguished road

Default Re: Php Redirecting Using Cookies

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.
Hatter Madigan is offline  
Old 07-18-2008, 04:17 PM   #6 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,690

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Php Redirecting Using Cookies

PHP Code:
<?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.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling all PHP and MySql experts murdocsvan Programming Discussions 4 04-17-2008 01:51 PM
HELP: Cookies HiJack HELP/Ques. script.kiddie Virus - Spyware Protection / Detection 0 12-28-2007 06:41 AM
PHP redirecting mil1243 Programming Discussions 4 04-30-2007 04:45 PM
PHP 2-player game? thejeremy Programming Discussions 4 04-09-2007 06:01 PM
Remote file access w/ IP + PHP? thejeremy Programming Discussions 2 04-04-2007 05:24 PM