Code specific redirect? E.g. Enter 123, redirected to /123.

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
Stevey_V3
Forum Newbie
Posts: 6
Joined: Sat Jul 25, 2009 4:18 pm

Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
Stevey_V3
Forum Newbie
Posts: 6
Joined: Sat Jul 25, 2009 4:18 pm

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
Stevey_V3
Forum Newbie
Posts: 6
Joined: Sat Jul 25, 2009 4:18 pm

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
Stevey_V3
Forum Newbie
Posts: 6
Joined: Sat Jul 25, 2009 4:18 pm

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
Stevey_V3
Forum Newbie
Posts: 6
Joined: Sat Jul 25, 2009 4:18 pm

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post 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.
Stevey_V3
Forum Newbie
Posts: 6
Joined: Sat Jul 25, 2009 4:18 pm

Re: Code specific redirect? E.g. Enter 123, redirected to /123.

Post by Stevey_V3 »

[wrong forum]
Post Reply