/    Sign up×
Community /Pin to ProfileBookmark

Redirect to another page using Header

Hello everyone,

I have a problem with redirecting the user to a different page using the function Header.I have a form where the user complete his/her details in order to register in the website(register.php).The page register.php checks to see if specific fields has been filled in and if the data type that the user entered is the correct ones.If the details are correct then a message appers welcoming the user and then redirecting to another page to the basket.php in order to buy the items.
In order to redirect to another page I used the “header”

[code=php]header (“Location: http://”. $_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘PHP_SELF’]) . “/basket.php”);[/code]

The problem is that when i use the header is not being redirected.
Here is the code for all the page:

[code=html]
<table width=”717″ border=”0″ align=”center” cellspacing=”4″>
<tr>
<td><span class=”generalfont”><img src=”registeronline.jpg” width=”185″ height=”19″ /><br><br>
Once you have registered, you will be able to log in to your account and buy online. We will also keep you up to date with our new ranges and special offers. <br>
<br>
Simply fill in the form or click autofill on your google toolbar. </span></td>
</tr>
</table>
<br>
<form name=”register” method=”post” action=”<? echo $_SERVER[‘PHP_SELF’]; ?>”>

<table width=”406″ border=”0″ align=”center”>
<tr>
<td><label class=”generalfont”>Title:</label></td>
<td>
<select name=”title” class=”generalfont” >
<option value=”Ms”>Ms</option>
<option value=”Mrs”>Mrs</option>
<option value=”Miss”>Miss</option>
<option value=”Mr”>Mr</option>
<option value=”Dr”>Dr</option>
<option value=”Prof”>Prof</option>
</select> </td>
<td><label class=”generalfont”>Address:</label></td>
<td><input name=”address” type=”text” class=”generalfont” ></td>
</tr>
<tr>
<td> <label class=”generalfont” >First Name:</label></td>
<td><input name=”firstname” type=”text” class=”generalfont” ></td>
<td><label class=”generalfont” >Town:</label></td>
<td><input name=”town” type=”text” class=”generalfont” ></td>
</tr>
<tr>
<td><label class=”generalfont” >Last Name:</label></td>
<td><input name=”lastname” type=”text” class=”generalfont”></td>
<td><label class=”generalfont” >County:</label></td>
<td><input name=”county” type=”text” class=”generalfont”></td>
</tr>
<tr>
<td><label class=”generalfont”>E-mail:</label></td>
<td><input name=”email” type=”text” class=”generalfont”></td>
<td><label class=”generalfont”>Postcode:</label></td>
<td><input name=”postcode” type=”text” class=”generalfont”></td>
</tr>
<tr>
<td><label class=”generalfont” >Username:</label></td>
<td><input name=”username” type=”text” class=”generalfont”></td>
<td><label class=”generalfont” >Telephone:</label></td>
<td><input name=”telephone” type=”text” class=”generalfont”></td>
</tr>
<tr>
<td><label class=”generalfont”>Password:</label></td>
<td><input name=”password” type=”password” class=”generalfont”></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label class=”generalfont”>Confirm password:</label></td>
<td><input name=”confirm” type=”password” class=”generalfont”></td>
<td colspan=”2″><input name=”submit” type=”image” src=”register.jpg” class=”generalfont” value=”Register” /></td>
</tr>
</table>
<table width=”406″ border=”0″ align=”center”>
<tr>
<td>
[/code]

[code=php]
<? if (isset($_POST[‘submit_x’]))
{//If submit button has been clicked

//First check if there is data and is valid

//check for the firstname
if (eregi (“^[[:alpha:].’ -]{2,15}$”, stripslashes(trim($_POST[‘firstname’]))))
{
$firstname= $_POST[‘firstname’];
}else {
$firstname= FALSE;
echo ‘<span class=”errorregister”>- Please enter your firstname</span> <br>’ ;

}
//check for the lastname
if (eregi(“^[[:alpha:].’ -]{2,30}$”, stripslashes(trim($_POST[‘lastname’]))))
{
$lastname= ($_POST[‘lastname’]);
}else {
$lastname= FALSE;
echo ‘<span class=”errorregister”> – Please enter your lastname </span><br>’;
}
//check for the email
if (eregi(“^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(.[a-zA-Z0-9-]+)*$”, stripslashes(trim($_POST[’email’]))))
{
$email=($_POST[’email’]);
}else {
$email= FALSE;
echo ” <span class=”errorregister”> – Please enter a valid email address </span> <br>”;
}
//check for the username
if (eregi(“^[[:alnum:]_]{4,20}$”, stripslashes(trim($_POST[‘username’]))))
{
$username=($_POST[‘username’]);
}else {
$username= FALSE;
echo “<span class=”errorregister” > – Please enter a valid username </span><br>”;
}
//check for the password
if (eregi(“^[[:alnum:]]{1,8}$”, stripslashes(trim($_POST[‘password’]))))
{
if ($_POST[‘password’]==$_POST[‘confirm’])
{
$password=($_POST[‘password’]);
}else
{
$password=FALSE;
echo “<span class=”errorregister”> – Your password did not match the confirmed password </span><br>”;
}
}
else {
$password=FALSE;
echo “<span class=”errorregister”> – Please enter a valid password </span><br>”;
}

if ($firstname && $lastname && $username && $password && $email)
{
$queryUsername=”Select * from Customers where username=’$username'”;
$rsUsername=@mysql_query($queryUsername);

if(mysql_num_rows($rsUsername)==0) //if it is available
{
//Add the user
$query=”Insert into users(`prefix`,`user`, `pass`, `first`, `last`, `email`,`address`, `town`,`county`,`postcode`,`tel`)
values (‘$title’,’$username’,’$password’,’$firstname’,’$lastname’,’$email’,’$_POST[address]’,’$_POST[town]’,
‘$_POST[county]’,’$_POST[postcode]’,’$_POST[telephone]’)”;
$rs=@mysql_query($query);//Run the query

if ($rs)//If it ran ok
{
echo “<span class=”generalfont”>”.$firstname .” thank you for signing in the website </span>”;
header (“Location: http://”. $_SERVER[‘HTTP_HOST’] . dirname($_SERVER[‘PHP_SELF’]) . “/basket.php”);

}
else
{ //if it was not run
echo “<p class=”errorregister”> You could not be registered. Sorry for any incovenience. </p>”;
}
}
else { //the username is not available
echo “<p class=”errorregister”> That username is already taken. Please choose another. </p>”;
}
mysql_close();
}
else { //if one of the data test failed
echo “<p class=”errorregister”> Please try again.</p>”;
}
}//end the submit conditional (if the user submit the form)
?>
</td>
</tr>
</table>

</form>
[/code]

I would appreciate if you could helped me solve it,either using header or any other idea.

Thanks,
Xenia

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@GarySJun 06.2006 — Note that header information needs to be sent before anything else. Think your "thank you" message is stopping the redirect.
Copy linkTweet thisAlerts:
@vevsJun 06.2006 — yes, header() should be at the very top before you print any messages. OR you can use a javascript redirect

<script language='javascript'>

window.location.href='http://www.gotodomain.com';

</script>
Copy linkTweet thisAlerts:
@xeniaauthorJun 06.2006 — I tryied to put the header first and erase the welcome note but still is not working.However,when i used the javascript redirect the user to another page but it didn't display the welcome note.

How I can display first the welcome note and then redirect the user?

Thank you
Copy linkTweet thisAlerts:
@vevsJun 06.2006 — use setTimeout for the javascript, so it redirects in 5 (or more) seconds.
Copy linkTweet thisAlerts:
@GarySJun 06.2006 — Don't forget to add a good ole fashioned hyperlink for the non-JavaScript-ers
Copy linkTweet thisAlerts:
@NogDogJun 06.2006 — You might want to look into using a "meta refresh" tag in your result page to do the redirect, so that you don't have to depend on the client having JavaScript available and enabled.
Copy linkTweet thisAlerts:
@xeniaauthorJun 06.2006 — I would be more interesting If i wasn't using javascript.

So how I can do it?

Could you please provide an example?

Thanks,

Xenia
Copy linkTweet thisAlerts:
@NogDogJun 06.2006 — Within the <head> section of any page:
<i>
</i>&lt;meta http-equiv="refresh" content="[color=red]5[/color];url=http://yoursite.com/new_page.html"&gt;

The number in red is the time in seconds before the page will redirect to the URI which follows it.
Copy linkTweet thisAlerts:
@xeniaauthorJun 07.2006 — Yes but with this code the page will redirected automaticaly after 5 pages.What I would like is when the user register then to be redirected in the other page
×

Success!

Help @xenia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...