(Newbie) redirect to specific page by unique id

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MarcB
Forum Newbie
Posts: 3
Joined: Tue Aug 04, 2009 5:11 pm

(Newbie) redirect to specific page by unique id

Post by MarcB »

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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: (Newbie) redirect to specific page by unique id

Post by McInfo »

Store the user names and file paths in an associative array where the user name is the key. Alternatively, the names and paths could be stored in a database table. Use header('Location: ...') to redirect to the user's page. You could use include() instead if you wanted to obscure the location of the page in the browser's address bar.

index.php

Code: Select all

<?php
$locations = array
(   'marc' => './page1.php'
,   'dave' => './page2.php'
);
if (isset($_GET['user']) && array_key_exists($_GET['user'], $locations)) {
    header('Location: ' . $locations[$_GET['user']]);
    exit;
}
?>
The URL

Code: Select all

index.php?user=marc
will be redirected to

Code: Select all

page1.php
To make the user name case-insensitive, use strtolower().

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:23 pm, edited 1 time in total.
MarcB
Forum Newbie
Posts: 3
Joined: Tue Aug 04, 2009 5:11 pm

Re: (Newbie) redirect to specific page by unique id

Post by MarcB »

Thanks for that, I am still a little confused though. My code looks like this modified from the above

Code: Select all

<?php
 $locations = array
 (   'DOE' => './DOE/page1.html'
 ,   'TELS' => './TELS/page2.html'
 );
 if (isset($_GET['user']) && array_key_exists($_GET['user'], $locations)) {
     header('Location: ' . $locations[$_GET['user']]);
      exit;
 }
 ?>
and my link in the htm form looks like this

Code: Select all

<form action="goto.php" method="get">
<INPUT type="text" name="user">
<input type="submit" value="Go">
</form>
this results in an address in the browser like this

Code: Select all

Example/goto.php?user=doe
but this does not redirect anywhere, I apologise for my lack of knowledge on the subject but I am trying to understand it.

Thanks in advance
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: (Newbie) redirect to specific page by unique id

Post by McInfo »

Strings are case-sensitive. $_GET['user'] holds a string. To make it case-insensitive so it will match an index (key) in the array, all of the array indexes must be the same case (upper or lower). Then the string in $_GET['user'] must be converted to the proper case to match the array indexes.

index.php - This script assumes that all of the array keys are lowercase. If the keys are uppercase, use strtoupper() instead of strtolower().

Code: Select all

<?php
$locations = array
(   'marc' => './page1.php'
,   'dave' => './page2.php'
);
if (isset($_GET['user'])) {
    $_GET['user'] = strtolower($_GET['user']);
    if (array_key_exists($_GET['user'], $locations)) {
        header('Location: ' . $locations[$_GET['user']]);
        exit;
    }
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 4:24 pm, edited 1 time in total.
MarcB
Forum Newbie
Posts: 3
Joined: Tue Aug 04, 2009 5:11 pm

Re: (Newbie) redirect to specific page by unique id

Post by MarcB »

Sweet, it works correctly now as I was un aware of case sensitivity. one last query how could I add to the code a does not equal command which would point to an error page if a username is entered incorrectly.

thanks for all your help McInfo
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: (Newbie) redirect to specific page by unique id

Post by McInfo »

MarcB wrote:how could I add to the code a does not equal command which would point to an error page if a username is entered incorrectly.
Anything appended to the end of that script will appear if $_GET['user'] does not match any of the array keys.

Edit: This post was recovered from search engine cache.
Post Reply