www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] If cookie not set redirect

  1. #1

    resolved [RESOLVED] If cookie not set redirect

    Hi guys im having a little problem with a script im doing, hoping if anyone has any ideas. Basically i set a cookie earlier on in my script, and im now on a page which i want to display if the user has the cookie, if the cookie is not there, i want them 2 be rediected to a diffeent page (signin.php)

    here is my script so far:
    PHP Code:
    <html>
    <body>
    <?php
       
    if (!isset($_COOKIE['user']))
          echo 
    "you are signed in";
          
       else {
       
    header "Location: signin.php" ); }
       
    ?>
    <br />Click <a href="signout.php">here</a> to log out
    </body>
    </html>
    It seems fine except that whe the cookie is not set, it doesnt redirect, bit pointless!

    any ideas?

    thanx i advaced

  2. #2

    Question Improvement

    ok i have slightly changed the script now, this was more of a method of checking to see if the cookie was set. When i delete the set cookie i get the below error message:

    "Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Mike\My Documents\Test Folder\Login-mytry\example1.php:3) in C:\Documents and Settings\Mike\My Documents\Test Folder\Login-mytry\example1.php on line 7"

    So something is still wrong with my header re-direct. I have been doing some research, do i need to use a meta re-direct? what ever that is?
    here is my new PHP code:

    PHP Code:
    <html>
    <body>
    <?php
    if (isset($_COOKIE["user"]))
      echo 
    "Welcome " $_COOKIE["user"] . "!<br />";
      else
    header("Location: signin.php");
    ?>
    Click <a href="signout.php">here</a> to log out
    </body>
    </html>
    Hope this helps.

  3. #3
    ok i seem to have fixed it! its because i was sending the tags <html> and <body> to the HTTP before the redirect! so if anyone is inerested here is my new code.

    <?php
    if (isset($_COOKIE["user"]))
    echo "Welcome " . $_COOKIE["user"] . "!<br />";
    else
    header("Location: signin.php");
    ?>
    <html>
    <body>
    Click <a href="signout.php">here</a> to log out
    </body>
    </html>
    And i works

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles