cookie

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandre

    cookie

    What's the best I can do to solve my trouble :

    I got a simple page index-tmp who include a code snippet who test if a cookie
    exist and if yes redirect to the user account page but the trouble
    is when I didn't 've a cookie the page still redirect to the user account page.

    here is the following code :

    <?
    $cookie = $_COOKIE["free-nickname"];
    if (!cookie == null) {
    echo '<script langage = "javascript ">';
    echo " document.locati on.href = 'index_utilisat eur.php';";
    echo '</script>;';
    }
    else {
    echo '<script langage = "javascript ">';
    echo " document.locati on.href = 'index.php';";
    echo '</script>;';
    }
    ?>

    thx in advance


  • Kartic

    #2
    Re: cookie

    Your line :
    if (!cookie == null) {

    should read:
    if (!$cookie == null)
    (You missed the $ before the cookie variable name)

    Thanks!

    Comment

    • Alexandre

      #3
      Re: cookie

      Damn I'm stupid thx Kartic :)

      "Kartic" <kartic.krishna murthy@gmail.co m> wrote:[color=blue]
      >Your line :
      >if (!cookie == null) {
      >
      >should read:
      >if (!$cookie == null)
      >(You missed the $ before the cookie variable name)
      >
      >Thanks!
      >
      >[/color]


      Comment

      • Philip Ronan

        #4
        Re: cookie

        Alexandre wrote:
        [color=blue]
        > What's the best I can do to solve my trouble :
        >
        > I got a simple page index-tmp who include a code snippet who test if a cookie
        > exist and if yes redirect to the user account page but the trouble
        > is when I didn't 've a cookie the page still redirect to the user account
        > page.
        >
        > here is the following code :
        >
        > <?
        > $cookie = $_COOKIE["free-nickname"];
        > if (!cookie == null) {
        > echo '<script langage = "javascript ">';
        > echo " document.locati on.href = 'index_utilisat eur.php';";
        > echo '</script>;';
        > }
        > else {
        > echo '<script langage = "javascript ">';
        > echo " document.locati on.href = 'index.php';";
        > echo '</script>;';
        > }
        > ?>
        >
        > thx in advance
        >
        >[/color]

        I think "if ($cookie != null)" might work better.

        Don't use Javascript to redirect visitors -- HTTP redirects are much safer.
        I suggest you make index.php your default home page and put this script at
        the top of the file:

        <?php

        $cookie = $_COOKIE["free-nickname"];
        if ($cookie != null) {
        header("HTTP/1.1 302 Found");
        header("Locatio n: http://www.yourdomain. com/index_utilisate ur.php");
        exit;
        }
        else {
        header("Vary: Cookie");
        }

        ....


        ?>

        P.S. To include javascript in an HTML file, use <SCRIPT
        type="text/javascript">, not <SCRIPT lang(u)age="jav ascript">

        --
        phil [dot] ronan @ virgin [dot] net



        Comment

        • Michael Fesser

          #5
          Re: cookie

          .oO(Alexandre)
          [color=blue]
          >I got a simple page index-tmp who include a code snippet who test if a cookie
          >exist and if yes redirect to the user account page [...][/color]

          Why are you using unreliable client-side redirection? Do it properly and
          use a Location header with an absolute URL:

          header('Locatio n: http://www.example.com/foo');

          Micha

          Comment

          • claudel@gmail.com

            #6
            Re: cookie

            With the Location header you can make sure that all your visitors will
            be redirected to the page you want to redirect them to. Think of
            lynx/links users who do not use a javascript enabled browser. Also,
            please note that the header() must go before any output is made on the
            page.

            Good luck
            Clau

            Comment

            • Alexandre

              #7
              Re: cookie

              great thx =)

              "Kartic" <kartic.krishna murthy@gmail.co m> wrote:[color=blue]
              >Your line :
              >if (!cookie == null) {
              >
              >should read:
              >if (!$cookie == null)
              >(You missed the $ before the cookie variable name)
              >
              >Thanks!
              >
              >[/color]


              Comment

              Working...