(Newbie) redirect to specific page by unique id

Discussion in 'PHP' started by mcdeere02, Aug 5, 2009.

Thread Status:
Not open for further replies.
  1. #1
    Hi,

    I'm pretty new to php and you guys may be able to help, what I am trying to do is create a php script that redirects a user to a specific page based on a username that they enter, no password needed. I am completely clueless when writing code such as this.

    e.g.

    user types name in html form such as "marc"

    the form references the php script which identifies "if marc" then redirects to page1.html , whereas "if dave" is entered it wouuld redirect to page2.html.

    is this possible?

    any suggestions or help would be greatfully recieved, thanks

    Marc
     
    mcdeere02, Aug 5, 2009 IP
  2. EverestTech

    EverestTech Well-Known Member

    Messages:
    554
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    140
    #2
    You can simply do like this
    
    <?php
    $name = $_POST['name'];
    if($name == "marc")
    {
    	header('Location: http://www.yoursite.com/page1.html');
    }
    elseif($name == "dave")
    {
    	header('Location: http://www.yoursite.com/page2.html');
    }
    else
    {
    	header('Location: http://www.yoursite.com/nopage.html');
    }
    ?>
    
    
    PHP:
    there are manyways but it's just a basic illustration for you hope it helps
     
    EverestTech, Aug 5, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or like this: (just to let you know another possible way, if you have many such names you wouldn't want to write all those if elses)

    
    $destination = array('marc' => 'page1.html', 'dave' => 'page2.html');
    if (isset($destination[$_POST['name'])) {
       header('Location: http://www.example.com/' . $destination[$_POST['name']]);
    } else {
       header('Location: http://www.example.com/default.html');
    }
    die();
    
    PHP:
     
    premiumscripts, Aug 5, 2009 IP
  4. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey,

    There are couple of methods of do that. These methods depends upon the conditions your have.


    if you have lots of condition then you need to use switch case

    $name = $_POST['name'];


    switch($name)
    {
    case 'marc': header("Location: the address to redirect");
    break;
    case 'dave': header("Location: the address to redirect");
    break;
    case 'john': header("Location: the address to redirect");
    break;
    }


    Now on the redirect thing you can use javascript redirect as well

    echo("<script>location.href='redirect location'</script>");


    Hope this works:

    Regards,
    Stylesofts Developing Team
     
    Stylesofts, Aug 6, 2009 IP
Thread Status:
Not open for further replies.