SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP get Cookies value and redirect

    Hi

    I have an app that uses ASP to set the value of cookie, and I aim to use PHP in a small section of the app to get the value of the cookie, and redirect according to the value of the cookie.

    lang is the name of the cookie, and "English" "French" are the value. If the value is English, then redirect to English page, other to French page.

    I have tried the below, but it does seem to get the value of the cookie, but it rather displays the first, which return true no matter what.

    Can anyone suggest?? Thanks

    PHP Code:
    <?php
    if (isset($_COOKIE["lang"] ) == "English")
    echo 
    "English";
    else
    echo 
    "French";
    ?>
    This is the asp code that I use to set the cookie: -

    Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <%
    Response.Cookies("lang")="French"
    %>

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // set a default
    $lang 'English';

    if (isset(
    $_COOKIE["lang"]) && $_COOKIE["lang"] == 'French') {
        
    $lang 'French';
    }


    // or
    $lang 'English';
    if (isset(
    $_COOKIE["lang"])) {
        if (
    $_COOKIE["lang"] == 'French') {
            
    $lang 'French';
        } elseif (
    $_COOKIE["lang"] == 'Spanish') {
            
    $lang 'Spanish';
        } 
    // etc...

    you could also use a switch() statement, or an array if you have a larger list.

  3. #3
    SitePoint Enthusiast MultiGfx's Avatar
    Join Date
    Jun 2006
    Location
    Tadley, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, This is the code you want:
    PHP Code:
    if ($_COOKIE["lang"] == "English")
    {
    echo 
    'English';
    } elseif (
    $_COOKIE["lang"] == "French") {
    echo 
    'French';

    PHP Code:
    <?php $steven "webmonkey"; echo ".$steven."?>

  4. #4
    SitePoint Enthusiast MultiGfx's Avatar
    Join Date
    Jun 2006
    Location
    Tadley, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    PHP Code:
    // set a default
    $lang 'English';

    if (isset(
    $_COOKIE["lang"]) && $_COOKIE["lang"] == 'French') {
        
    $lang 'French';

    hmm.. He never said he wanted a default, but i think my code would work better!!

    sorry
    PHP Code:
    <?php $steven "webmonkey"; echo ".$steven."?>

  5. #5
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hei!...quick, thanks.....

    I'll try it right now! cool

  6. #6
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic. It works. Gonna try to combine it with the HTTP_ACCEPT_LANG dir, so if the browsers lang is either, then redirect. But firstly, I want to check that cookie exists. Not to sure on PHP, but getting used to the syntax...

    Anyway...thanks!

  7. #7
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MultiGfx
    hmm.. He never said he wanted a default, but i think my code would work better!!

    sorry
    The default will be English..., so if no cookie, and no HTTP header, then English...

    ai...

  8. #8
    SitePoint Enthusiast MultiGfx's Avatar
    Join Date
    Jun 2006
    Location
    Tadley, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    whose worked out of interested?
    PHP Code:
    <?php $steven "webmonkey"; echo ".$steven."?>

  9. #9
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MultiGfx, your code will produce notices in the absence of the cookie.

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // assume this unless overwritten
    $lang 'English';

    // if the HTTP_ACCEPT_LANG var exists
    if (isset($_SERVER['HTTP_ACCEPT_LANG'])) {
        
    // case insensitive sub string search
        
    if (stristr($_SERVER['HTTP_ACCEPT_LANG'], 'en')) {
            
    $lang 'English';
        } elseif (
    stristr($_SERVER['HTTP_ACCEPT_LANG'], 'fr')) {
            
    $lang 'French';
        }
    }

    // if we have a cookie, lets potentially overwrite the value
    if (isset($_COOKIE['lang'])) {
        if (
    $_COOKIE['lang'] == 'English') {
            
    $lang 'English';
        } elseif (
    $_COOKIE['lang'] == 'French') {
            
    $lang 'French'
        }
    }

    echo 
    $lang
    some if it is redundant, but i left it in for clarity.

    also, im not sure if its safe to check for the 'fr' sub string to detect french, but you get the point.

  11. #11
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hei there

    http://www.sitepoint.com/forums/show...82#post3158182

    Just posted the whole working code here.

    The only one I tested was MultiGfx's code, which worked, but I will take a look at clamcrusher's example, since it seems more solid perhaps.

    Not sure..., but if it works, thats great.

    ?

  12. #12
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, the next 'thing' I wanna see is, if the browser priorities's* the HTTP lang dir over the cookie. But for the app, the cookie, hopefully takes priority.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •