SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cookie content redirects users using PHP

    Good morning guys!

    Well I am new to PHP (as most ones here). I have ugly system developed but should stick to it.

    I have index.php file on www.somename.com with two (there will be more) text links:

    <a href="http://www.somethinggerman.com/german.php">German</a>
    <a href="http://www.somethingrussian.com/russian.php">Russian</a>

    and a PHP code on top of <HTML> tag:

    <?php
    if ( $_COOKIE['langSet'] == ger)
    header("Location: http://www.somethinggerman.com/german.php");
    elseif ( $_COOKIE['langSet'] == rus)
    header("Location: http://www.somethingrussian.com/russian.php");
    ?>

    which simply checks for contents of cookie and sends user to site on his language.

    And I have two more files: german.php and russian.php with a PHP code on top of <HTML> tag:

    <?php
    $lang = 'ger';
    setcookie("langSet", $lang, time()+60);
    ?>

    for german.php
    and

    <?php
    $lang = 'rus';
    setcookie("langSet", $lang, time()+60);
    ?>

    for russian.php

    In each of those files there is a text link:
    <a href="http://www.somename.com/index.php">Reselect language</a>

    Main point in all this is that once user has selected his language, next time he comes to this site he is sent to site in his language. I am sorry for being so long. Here is my problem, guys...

    If user has selected wrong language and want's to change it, there is no way to do so, since as he comes back to http://www.somename.com/index.php he is sent immediately to the site in language he selected before.

    How should I rewrite this <a href="http://www.somename.com/index.php">Reselect language</a> link and what should I add to PHP code in index.php page, so it does not redirect user if he wants to change the language?

    I am really desperate about this sitting wor a week now trying to find a solution... Pleas could you help me?

  2. #2
    PHP Brainiac dg_den_golotyuk's Avatar
    Join Date
    Jul 2006
    Location
    Kiev, Ukraine
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In each of those files there is a text link:
    <a href="http://www.somename.com/index.php">Reselect language</a>
    Maybe you need to use some 3rd page - exactly for reselecting the language. For example you could use link like:
    HTML Code:
    <a href="http://www.somename.com/reselect.php">Reselect language</a>
    And reselect.php will contain:
    PHP Code:

    setcookie
    ("langSet"""time()+60);
    header("Location: index.php"); 
    DG [Den Golotyuk], Lead Developer
    Chestnut Software
    Avoid web outsourcing scams!
    Click here
    for a free downloadable report

  3. #3
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could pass a query variable in the link to tell it not to redirect for this request
    link
    <a href="http://www.somename.com/index.php?redirect=no">Reselect language</a>
    index.php
    PHP Code:
    <?php
    if(!isset($_GET['redirect'] != "no") {
     if ( 
    $_COOKIE['langSet'] == ger)
     
    header("Location: http://www.somethinggerman.com/german.php");
     elseif ( 
    $_COOKIE['langSet'] == rus)
     
    header("Location: http://www.somethingrussian.com/russian.php");
    }
    ?>

  4. #4
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Amazing... I really did not know that you can actually create a link with '?variablename=itsvalue" and that the you can get this variable and it's value using $_GET ... wow... it is what.. it's like I declare variable and it's value in a html link?

    I am so stupid. Oh... right I am new to this... Thank you alot guys!

  5. #5
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    You could pass a query variable in the link to tell it not to redirect for this request
    link
    <a href="http://www.somename.com/index.php?redirect=no">Reselect language</a>
    index.php
    PHP Code:
    <?php
    if(!isset($_GET['redirect'] != "no") {
     if ( 
    $_COOKIE['langSet'] == ger)
     
    header("Location: http://www.somethinggerman.com/german.php");
     elseif ( 
    $_COOKIE['langSet'] == rus)
     
    header("Location: http://www.somethingrussian.com/russian.php");
    }
    ?>
    Hm... look it is fantastic idea, but something still does not want to work
    if I make the link like you said, and put this extra line of code in index.php
    I don't get to see contents of index.php at all... it shows empty page. Why is that?

  6. #6
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, now I am testing it, and it is not working.

    After I put this in my index.php

    <?php
    if(!isset($_GET['redirect'] != "no") {
    if ( $_COOKIE['langSet'] == ger)
    header("Location: http://www.somethinggerman.com/german.php");
    elseif ( $_COOKIE['langSet'] == rus)
    header("Location: http://www.somethingrussian.com/russian.php");
    }
    ?>

    and link in a language page with ?redirect=no at the end, it goes to index.php?redirect=no but page is blank. shows nothing.

    I don't get it... and shouldn't there be an extra ")" after != "no") ?

  7. #7
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry i mis-typed, that would cause a syntax error
    PHP Code:
    <?php
    if($_GET['redirect'] != "no") {
    if ( 
    $_COOKIE['langSet'] == ger)
    header("Location: http://www.somethinggerman.com/german.php");
    elseif ( 
    $_COOKIE['langSet'] == rus)
    header("Location: http://www.somethingrussian.com/russian.php");
    }
    ?>
    It should have given an error though... make sure error_reporting is turned on.

  8. #8
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well now it gives me a bunch of errors:

    Notice: Undefined index: redirect in C:\apache2triad\htdocs\TEST\index.php on line 2

    Notice: Undefined index: langSet in C:\apache2triad\htdocs\TEST\index.php on line 3

    Notice: Use of undefined constant ger - assumed 'ger' in C:\apache2triad\htdocs\TEST\index.php on line 3

    Notice: Undefined index: langSet in C:\apache2triad\htdocs\TEST\index.php on line 5

    Notice: Use of undefined constant rus - assumed 'rus' in C:\apache2triad\htdocs\TEST\index.php on line 5

    God...

  9. #9
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I even made it as dg_den_golotyuk told me to do, and it works,
    but it still gives error messages as I enter index.php page:

    Notice: Undefined index: langSet in C:\apache2triad\htdocs\TEST\index.php on line 2
    Notice: Undefined index: langSet in C:\apache2triad\htdocs\TEST\index.php on line 4


    Maybe those notices are not errors at all and I should not pay any attention to them, just configure my php.ini so it does not show NOTICES?

    Anyway, it is ugly way of making thing work. I prefer the way gavwvin told me to go, but it seems there is still something wrong. Any ideas?

  10. #10
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, just turn the level of error reporting down so you don't get notices- most of those are not really anything to worry about and would show up in a lot of people's scripts if you turned notices on... although I would change ger to 'ger' and rus to 'rus' as it suggests... you mean them as strings not constants. It should be fine then.

  11. #11
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. Thank you. I will try it tomorrow and will let you know guys!

  12. #12
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah you could turn error reporting down a notch if you like, although preferably you should work towards learning how to code in a way that wont produce notices.

    but remember this, if your writing or debugging code, its VERY usefull to set error reporting to E_ALL so that php will tell you when you use an undefined variable. you will be suprised how many problems you can solve by yourself if you turn up error reporting while you debug.

    some common notices
    - undefined variable, self explanatory, the variable has not been defined, eg doesnt exist. example: echo $foo;

    - undefined index. similar to above, but it means the element in the array doesnt exist. example: $_GET['foo'] doesnt exist. $_GET is an array, 'foo' is the index.

    - undefined constant, it means you forgot to quote a string. example: if ($foo == bar) should be if ($foo == 'bar')

  13. #13
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, but if we are coming back to the problem I in fact still have then, if I understand it correct, the LINK I should put on every language page from where I would like to get back to language selection page:

    <a href="http://www.somename.com/index.php?redirect=no">Reselect language</a>

    can not be just placed in this page, I also should define this "redirect" as a variable and "no" as it's value on the same page somewhere?
    So when there is a click on this link and it takes me back to index.php, a $_GET knows what this redirect=no means, correct?

    so in other words, in the same page where this link is, I should put:
    $redirect = 'no';

    is it true?

  14. #14
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, and could somebody explain me in english (not PHP) what is going on here:


    <?php
    if($_GET['redirect'] != "no")

    {
    if ( $_COOKIE['langSet'] == 'ger')
    header("Location: http://www.somethinggerman.com/german.php");
    elseif ( $_COOKIE['langSet'] == 'rus')
    header("Location: http://www.somethingrussian.com/russian.php");
    }

    ?>

    is it - If I (code) receive variable 'redirect' with value 'no' then I... do nothing,
    but if I there is a cookie 'LangSel' with value 'ger' set, I should redirect user to location "...." and so on... Do I understand it right?
    If it is so, I don't get why there is nothing after variable 'redirect' is received by $_GET ? Shouldn't I put something like:
    header("Location: http://www.somename.com/index.php"); ?

  15. #15
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you load any php page, if you put ?var=value&othervar=othervalue after the filename, then the in the page itself the value of $_GET['var'] will be "value" and $_GET['othervar'] = "othervalue".
    So if you load your index page with ?redirect=no, $_GET['redirect'] = "no" in the script. If the page is loaded without anything after the file name, $_GET['redirect'] is undefined
    If I (code) receive variable 'redirect' with value 'no' then I... do nothing,
    but if I there is a cookie 'LangSel' with value 'ger' set, I should redirect user to location "...." and so on... Do I understand it right?
    Thats pretty much it... i'm sorry i assumed there was more after that on index.php... you need to put after that bit of code a page which has a language select.
    PHP Code:
    <?php
    if($_GET['redirect'] != "no")
    {
    if ( 
    $_COOKIE['langSet'] == 'ger')
    header("Location: http://www.somethinggerman.com/german.php");
    elseif ( 
    $_COOKIE['langSet'] == 'rus')
    header("Location: http://www.somethingrussian.com/russian.php");
    }
    //if the code gets here it means redirect = no, so show a langauge select page
    ?>
    <html>
    <body>
    Select Your language<br />
    <a href="/ger.php">German</a><br />
    <a href="/russian.php">Russian</a>
    </body>
    </html>

  16. #16
    SitePoint Member
    Join Date
    Aug 2006
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, sorry for being so annoying but I just want to learn it and get clear.

    There '{' brackets isolate what is inside them from
    if($_GET['redirect'] != "no") string, true?

    so if browser gets link with ?redirect=no at the end, it just skips what's inside
    { } and shows contents on page. Am I right?

    Thank you anyway. Will test it today and see if it all works well.

    p.s. What happens if it is a first visit to index.php page? If there is no cookie set and url was without '?redirect=no'? Then it will just skip the PHP code without any errors?

  17. #17
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes thats right it will skip it if there is a ?redirect=no, if there isn't and its their first visit,

    if ( $_COOKIE['langSet'] == 'ger') will be false so it won't redirect to ger.php and
    if ( $_COOKIE['langSet'] == 'rus') will be false so it won't redirect to rus.php

    So he program execution will skip both redirect statements and carry on to show the language select page


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
  •