Page 1 of 1
Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sat Jul 25, 2009 4:25 pm
by Stevey_V3
I'm setting up a website and part of that website concerns clients and their respective projects.
What I want do is make a code specific redirection. I'll explain in more detail: I want to make a page where the client has to enter a code such as 123456. I then want that code to be run through a script where that code is recognised, when the code is recognised, I want the client get redirected to a pre-defined directory of my choice such as /123456 or a page 123456.htm.
To put it simply: enter the code 123456, get redirected to /123456
I'm a HTML developer who's moving onto PHP but just needs a little help, correct me if I used the wrong terms above and please do ask further if you do not fully understand; I'm not good at explaining things.
Thank you. Steven.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sat Jul 25, 2009 4:30 pm
by omniuni
Hey, there! Don't worry, that's not too difficult.
Make a form that posts back using method="get".
Read the variable, and if it has been posted add an HTTP redirect to example.com/(your variable)
If you need more specifics, let me know.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sat Jul 25, 2009 5:01 pm
by Stevey_V3
omniuni wrote:Hey, there! Don't worry, that's not too difficult.
Make a form that posts back using method="get".
Read the variable, and if it has been posted add an HTTP redirect to example.com/(your variable)
If you need more specifics, let me know.
I'm very new to PHP only knowing a few commands like echo and that, so specifics would be a appreciated. Thank you.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sun Jul 26, 2009 3:27 am
by omniuni
Don't worry, you don't need much more than an echo.
Code: Select all
<?php
if(isset($_GET['directme'])){
$goToHere = $_GET['directme'];
echo "HTML for redirect to $goToHere";
}
?>
<form method="get">
<input name="directme">
<input type="submit">
</form>
Go ahead and put that into a page, (a blank page will do), and you should see how it works. It lets you capture what the user enters into the form, and then write out code using that. There are several ways to redirect a user once you have the data, one uses meta data, for another check out
http://us3.php.net/manual/en/function.header.php
Let me know if you need more specifics, but at least see if you can understand how this code works first.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sun Jul 26, 2009 7:11 am
by Stevey_V3
Ah thank you very much, I changed the code a little after reading that link you gave me to:
Code: Select all
<?php
if(isset($_GET['id'])){
$url = $_GET['id'];
header("Location: $url");
}
?>
Which works fine, but I would like to include an else statement so the user gets redirected to the same page again and is told that their ID wasn't found, would be be something like this:
Code: Select all
<?php
if(isset($_GET['id'])){
$url = $_GET['id'];
header("Location: $url");
}
else {
header("Location: .");
}
?>
Which doesn't work, so what I am missing? I really appreciate all of your help, thank you.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sun Jul 26, 2009 1:53 pm
by Stevey_V3
Oh and if you want to see it in action, you can here:
http://clearboth.co.uk/clients/
And enter the ID: 2009260701
Thanks for your help. Steven.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Sun Jul 26, 2009 9:02 pm
by omniuni
Code: Select all
<?php
if(isset($_GET['id']) && is_dir($_GET['id'])){
$url = $_GET['id'];
header("Location: $url");
}
?>
How's that work for you?
Oh, also, I would recommend adding a "submit" or "go" button... just because some people don't think to hit "enter".
Also, good job! Forums like these are here to help, but so many people come here just expecting to be told the answer. It's good to see someone who's willing to at least take a few minutes to think on their own. As usual, let me know if you have any more questions.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Wed Jul 29, 2009 12:30 am
by Stevey_V3
omniuni wrote:Code: Select all
<?php
if(isset($_GET['id']) && is_dir($_GET['id'])){
$url = $_GET['id'];
header("Location: $url");
}
?>
How's that work for you?
Oh, also, I would recommend adding a "submit" or "go" button... just because some people don't think to hit "enter".
Also, good job! Forums like these are here to help, but so many people come here just expecting to be told the answer. It's good to see someone who's willing to at least take a few minutes to think on their own. As usual, let me know if you have any more questions.
The code I post a few days back is what I'm using now and it's good, simple and does what I want it to do. Thanks for linking me to those articles, good to add on bits of code and do good old trial and error to get things working.
Steven.
Re: Code specific redirect? E.g. Enter 123, redirected to /123.
Posted: Wed Aug 12, 2009 12:18 am
by Stevey_V3
[wrong forum]