PHP redirect

Discussion in 'PHP' started by sg552, Sep 5, 2008.

  1. #1
    Hi,

    I don't know if this is the correct title, please accept my apology.

    I have an index.php (main page) and content.php (my content) page. If visitor visiting my website, when they return they simply click on the url bar and go to the content.php bypassing index.php

    My question is how to block user from directly viewing my content at content.php and redirect them to index.php. Only after they click a link on index.php they will be redirect to content.php. eg: at index.php: click here to view content and they will be redirect to content.php

    I already use javascript but when I turn off my javascript browser my website does not redirect so I think php might help...

    Thanks in advance
    :)
     
    sg552, Sep 5, 2008 IP
  2. pubdomainshost.com

    pubdomainshost.com Peon

    Messages:
    1,277
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use Session, set a variable in index.php and test if the variable is present in content.php,

    if content.php doesn't detect the session variable - do a redirect to index.php
     
    pubdomainshost.com, Sep 5, 2008 IP
    sg552 likes this.
  3. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Simple example using sessions.

    Index.php (at the top)

    
    session_start(); 
    $_SESSION["first_time"] = 1;
    
    PHP:
    Content.php (at the top, before all code)

    
    if($_SESSION["first_time"] != 1)
    {
        header("Location: index.php");
    }
    
    PHP:
    Regards,

    Steve
     
    Steve136, Sep 5, 2008 IP
    sg552 likes this.
  4. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks to both of you, the code works great :cool:

    thanks
    :)
     
    sg552, Sep 5, 2008 IP