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
(Newbie) redirect to specific page by unique id
Moderator: General Moderators
Re: (Newbie) redirect to specific page by unique id
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
The URL
will be redirected to
To make the user name case-insensitive, use strtolower().
Edit: This post was recovered from search engine cache.
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;
}
?>Code: Select all
index.php?user=marcCode: Select all
page1.phpEdit: 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.
Re: (Newbie) redirect to specific page by unique id
Thanks for that, I am still a little confused though. My code looks like this modified from the above
and my link in the htm form looks like this
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
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;
}
?>Code: Select all
<form action="goto.php" method="get">
<INPUT type="text" name="user">
<input type="submit" value="Go">
</form>Code: Select all
Example/goto.php?user=doeThanks in advance
Re: (Newbie) redirect to specific page by unique id
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().
Edit: This post was recovered from search engine cache.
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;
}
}
?>
Last edited by McInfo on Wed Jun 16, 2010 4:24 pm, edited 1 time in total.
Re: (Newbie) redirect to specific page by unique id
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
thanks for all your help McInfo
Re: (Newbie) redirect to specific page by unique id
Anything appended to the end of that script will appear if $_GET['user'] does not match any of the array keys.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.
Edit: This post was recovered from search engine cache.