Page 1 of 1
(Newbie) redirect to specific page by unique id
Posted: Tue Aug 04, 2009 5:23 pm
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
Re: (Newbie) redirect to specific page by unique id
Posted: Tue Aug 04, 2009 5:35 pm
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
will be redirected to
To make the user name case-insensitive, use strtolower().
Edit: This post was recovered from search engine cache.
Re: (Newbie) redirect to specific page by unique id
Posted: Wed Aug 05, 2009 1:45 pm
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
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
Re: (Newbie) redirect to specific page by unique id
Posted: Wed Aug 05, 2009 2:08 pm
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.
Re: (Newbie) redirect to specific page by unique id
Posted: Wed Aug 05, 2009 5:32 pm
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
Re: (Newbie) redirect to specific page by unique id
Posted: Wed Aug 05, 2009 7:05 pm
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.