Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Redirect

John
Guest
 
Posts: n/a
#1: Jan 15 '07
Hello,


I have a php contact script and I want it to redirect to my homepage
without using the standard header command.


<?php
header("location:http://www.johndoe.com/index.html");
exit;
?>




Does anyone know a way to do this?

Arjen
Guest
 
Posts: n/a
#2: Jan 15 '07

re: PHP Redirect


John schreef:
Quote:
Hello,
>
I have a php contact script and I want it to redirect to my homepage
without using the standard header command.
>
<?php
header("location:http://www.johndoe.com/index.html");
exit;
?>
>
Does anyone know a way to do this?
Use meta refresh
<meta http-equiv="refresh" content="0;URL=http://www.url.ext/" />

Aldough it makes that nasty double clicking noise on some systems.

--
Arjen
http://www.hondenpage.com
Lixas
Guest
 
Posts: n/a
#3: Jan 15 '07

re: PHP Redirect


Hi John, I have php function that can be used for users browser
redirection:
<?
function redirect($location, $type="header")
{
if ($type == "header")
{
header("Location: ".$location);
exit;
}
elseif($type == "script")
{
echo "<script
type='text/javascript'>document.location.href='".$location."' </script>\n";
die("redirecting: please <a href=\"$location\">click here</aif you
are not redirected in 5 sec.");
}
elseif($type == "meta")
{
die("
<html><head><title>Redirecting...</title>
<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3; URL=$location\">
</head>
<body>redirecting: please <a href=\"$location\">click here</aif you
are not redirected in 5 sec</body>
</html>");
}
}

// usage
redirect("www.yourserver.com/yourpage"); //this will use php header
type of redirection

redirect("www.yourserver.com/yourpage", "script") //this will create
javascript with redirections instruction for user and link in case of
JS is turned off

redirect("www.yourserver.com/yourpage", "meta") //this will redirect
your user to your page using meta tags and also creates link to
destination page in case of some kind of error
?>

I think that is all types of redirecting user to other page

John
Guest
 
Posts: n/a
#4: Jan 15 '07

re: PHP Redirect


On Mon, 15 Jan 2007 19:38:57 +0100, Arjen <dont@mail.mewrote:
Quote:
>John schreef:
Quote:
>Hello,
>>
>I have a php contact script and I want it to redirect to my homepage
>without using the standard header command.
>>
><?php
>header("location:http://www.johndoe.com/index.html");
>exit;
>?>
>>
>Does anyone know a way to do this?
>
>Use meta refresh
><meta http-equiv="refresh" content="0;URL=http://www.url.ext/" />
>
>Aldough it makes that nasty double clicking noise on some systems.



Thank you sir - that worked perfect.
Michael Fesser
Guest
 
Posts: n/a
#5: Jan 15 '07

re: PHP Redirect


..oO(John)
Quote:
>I have a php contact script and I want it to redirect to my homepage
>without using the standard header command.
>
>
><?php
>header("location:http://www.johndoe.com/index.html");
>exit;
>?>
That's the correct way. What's wrong with it?

Micha
Michael Fesser
Guest
 
Posts: n/a
#6: Jan 15 '07

re: PHP Redirect


..oO(Arjen)
Quote:
>Use meta refresh
><meta http-equiv="refresh" content="0;URL=http://www.url.ext/" />
>
>Aldough it makes that nasty double clicking noise on some systems.
And breaks the back button ...

Micha
Arjen
Guest
 
Posts: n/a
#7: Jan 16 '07

re: PHP Redirect


Michael Fesser schreef:
Quote:
.oO(John)
>
Quote:
>I have a php contact script and I want it to redirect to my homepage
>without using the standard header command.
>>
>>
><?php
>header("location:http://www.johndoe.com/index.html");
>exit;
>?>
>
That's the correct way. What's wrong with it?
>
Micha
I'm guessing headers allready sent error.

If op could show some code it would be clear :-)

--
Arjen
http://www.hondenpage.com - Mijn site over honden
Petr Vileta
Guest
 
Posts: n/a
#8: Jan 16 '07

re: PHP Redirect


"Arjen" <dont@mail.mepíse v diskusním príspevku
news:eohv3v$nbl$1@brutus.eur.nl...
Quote:
Michael Fesser schreef:
Quote:
>.oO(John)
>>
Quote:
>>I have a php contact script and I want it to redirect to my homepage
>>without using the standard header command.
>>>
>>>
>><?php
>>header("location:http://www.johndoe.com/index.html");
>>exit;
>>?>
>>
>That's the correct way. What's wrong with it?
>>
>Micha
>
I'm guessing headers allready sent error.
>
If op could show some code it would be clear :-)
>
Because header must be send BEFORE anything other. Sample code

<?php
if( some condition) {
header("Location: www.google.com"); }
else {
// read some from database }
// read or set session
?>
<html><head>
.....

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)



Michael Fesser
Guest
 
Posts: n/a
#9: Jan 16 '07

re: PHP Redirect


..oO(Arjen)
Quote:
>Michael Fesser schreef:
Quote:
>.oO(John)
>>
Quote:
>><?php
>>header("location:http://www.johndoe.com/index.html");
>>exit;
>>?>
>>
>That's the correct way. What's wrong with it?
>>
>I'm guessing headers allready sent error.
Then it would be time to fix the script instead of using ugly and
unreliable client-side redirects.
Quote:
>If op could show some code it would be clear :-)
Yep.

Micha
John
Guest
 
Posts: n/a
#10: Jan 16 '07

re: PHP Redirect


On Tue, 16 Jan 2007 08:33:18 +0100, Arjen <dont@mail.mewrote:
Quote:
>Michael Fesser schreef:
Quote:
>.oO(John)
>>
Quote:
>>I have a php contact script and I want it to redirect to my homepage
>>without using the standard header command.
>>>
>>>
>><?php
>>header("location:http://www.johndoe.com/index.html");
>>exit;
>>?>
>>
>That's the correct way. What's wrong with it?
>>
>Micha
>
>I'm guessing headers allready sent error.
>
>If op could show some code it would be clear :-)


Here is the code:



<?php

// Define your email address - where to send messages - here
define("MAIL_TARGET","johndoe@nowhere.com");

// Here you can redefine error messages
define("errorName","Invalid name! It must be at least 2 characters
long");
define("errorEmail","Invalid email address!");
define("errorMsg","Invalid message! It must be at least 10 characters
long");

function
createForm($name="",$email="",$message="",$error1= "",$error2="",$error3=""){
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post">
<table>
<tr><td>Name: </td><td class="error"><?php echo $error1;
?></td></tr>
<tr><td colspan="2"><input class="text" type="text"
name="name" value="<?php echo $name; ?>"></td></tr>
<tr><td>Email:</td><td class="error"><?php echo $error2;
?></td></tr>
<tr><td colspan="2"><input class="text" type="text"
name="email" value="<?php echo $email; ?>"></td></tr>
<tr><td>Message:</td><td class="error"><?php echo $error3;
?></td></tr>
<tr><td colspan="2"><textarea cols="40" rows="6"
name="message"><?php echo $message; ?></textarea></td></tr>
<tr><td colspan="2"><br/><input class="text" type="submit"
name="submitBtn" value="Send"></td></tr>
</table>
</form>
<?php
}

function isValidEmail($email){
$pattern =
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}

function sendMail($name,$email,$message){

$subject = "Message from website";
$from = "From: $name <$email>\r\nReply-To: $email\r\n";
$header = "MIME-Version: 1.0\r\n"."Content-type: text/html;
charset=iso-8859-1\r\n";
$content = htmlspecialchars($message);

$content = wordwrap($content,70);
mail(MAIL_TARGET,$subject,$content,$from.$header);

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Contact Form</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
<div id="caption">Contact us</div>
<div id="icon">&nbsp;</div>
<?php if (!isset($_POST['submitBtn'])) {
createForm();
} else {
$name = isset($_POST['name']) ? $_POST['name'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";

$error = false;

if (strlen($name)<2) {
$error = true;
$error1 = errorName;
}
if (!isValidEmail($email)) {
$error = true;
$error2 = errorEmail;
}
if (strlen($message)<10) {
$error = true;
$error3 = errorMsg;
}

if ($error){
createForm($name,$email,$message,$error1,$error2,$ error3);
}
else {
sendMail($name,$email,$message);
?>

<div id="result">
<table width="100%">
<tr><td>
Thanks for your message!
</td></tr>
</table>
</div>


<?php
}
}
?>
<div>
</body>
John Dunlop
Guest
 
Posts: n/a
#11: Jan 16 '07

re: PHP Redirect


Arjen:
Quote:
Use meta refresh
<meta http-equiv="refresh" content="0;URL=http://www.url.ext/" />
The meta refresh can be bewildering, all the more so if
the user isn't looking at the page when it "redirects". It
can knock the user's mental model out of kilter.

And, as Michael pointed out, it can also break the Back
button, because as soon as you go Back to the page with the
meta refresh, it "redirects" you again. Hitting the Back
button twice with a delay of zero is tricky.

The meta refresh also doesn't offer different types of
"redirect" - for example, permanent, temporary - but only one
fake redirect, lodged in the markup of the page.

And some browsers don't even support the meta refresh,
still others allow you to turn it off, so you would have to
offer another means of getting to the new page, such as a link.

What do you gain from the meta refresh and would it not be
better to either simply link to the new page or set up a real
redirect?

--
Jock

Carl Pearson
Guest
 
Posts: n/a
#12: Jan 16 '07

re: PHP Redirect


John wrote:
Quote:
Hello,
>
>
I have a php contact script and I want it to redirect to my homepage
without using the standard header command.
>
>
<?php
header("location:http://www.johndoe.com/index.html");
exit;
?>
>
>
>
>
Does anyone know a way to do this?
You could do it on the client via javascript, but that would mean an
additional call to the server (assuming the redirection is going to
somewhere else on the same site):

function Redirect($Url = "/")
{
echo "<script
language='JavaScript'>document.location.href='".$U rl."'</script>";
exit;
}
Arjen
Guest
 
Posts: n/a
#13: Jan 16 '07

re: PHP Redirect


John schreef:
Quote:
<?php if (!isset($_POST['submitBtn'])) {
createForm();
} else {
<snip>
Quote:
else {
sendMail($name,$email,$message);
Try something like this on top of ur page

if (!isset($_POST['submitBtn'])
{
// do ur mail check etc
sendMail($name,$email,$message);
header('Location: www.example.com');
exit;
}
else
{
//other stuff
}

<html>
etc

--
Arjen
http://www.hondenpage.com
dan@tobias.name
Guest
 
Posts: n/a
#14: Jan 25 '07

re: PHP Redirect


On Jan 16, 10:42 am, John <John_nos...@nnnnnnnnn.nowherewrote:
Quote:
}function isValidEmail($email){
$pattern =
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
>
if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
OK, this isn't the main topic of this thread, but I have to remark
about the above function which was posted as an example of the code
John is using; it appears that this requires as one aspect of a "valid"
e-mail address that it have a top-level domain that is exactly 2 or 3
characters long, which would exclude addresses in .info, .name,
..museum, and some other TLDs.

--
Dan

Rik
Guest
 
Posts: n/a
#15: Jan 25 '07

re: PHP Redirect


On Thu, 25 Jan 2007 22:15:43 +0100, <dan@tobias.namewrote:
Quote:
On Jan 16, 10:42 am, John <John_nos...@nnnnnnnnn.nowherewrote:
Quote:
>}function isValidEmail($email){
> $pattern =
>"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
>>
> if (eregi($pattern, $email)){
> return true;
> }
> else {
> return false;
> }
>
OK, this isn't the main topic of this thread, but I have to remark
about the above function which was posted as an example of the code
John is using; it appears that this requires as one aspect of a "valid"
e-mail address that it have a top-level domain that is exactly 2 or 3
characters long, which would exclude addresses in .info, .name,
.museum, and some other TLDs.
Yup, I've given up trying to regex valid emaildresses (try to find
Friedl's total email validation regex, it's a terrible beast). There are
more characters allowed then just [a-z0-9_.-]. I'm just checking if the
adress contains one and only one @, and wether I can locate the domain
from the server.

--
Rik Wasmus
* I'm testing several new newsreaders at the moment. Please excuse
possible errors and weird content. *
Michael Fesser
Guest
 
Posts: n/a
#16: Jan 25 '07

re: PHP Redirect


..oO(Rik)
Quote:
>Yup, I've given up trying to regex valid emaildresses (try to find
>Friedl's total email validation regex, it's a terrible beast). There are
>more characters allowed then just [a-z0-9_.-]. I'm just checking if the
>adress contains one and only one @, and wether I can locate the domain
from the server.
I like this one ... ;)

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

Micha
Closed Thread