Connecting Tech Pros Worldwide Forums | Help | Site Map

Redirect

Purple Haze
Guest
 
Posts: n/a
#1: Jul 17 '05
ASP has <%Response.redirect("http://blahblah")%>

What is the equivalent command in php to redirect a user to a new URL in
the same window? Or is there a Javascript command that does this job better?

Thanks,
Purple Haze

Michael Fesser
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Redirect


.oO(Purple Haze)
[color=blue]
>ASP has <%Response.redirect("http://blahblah")%>
>
>What is the equivalent command in php to redirect a user to a new URL in
>the same window? Or is there a Javascript command that does this job better?[/color]

http://www.php.net/header

Micha
Purple Haze
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Redirect


Michael Fesser wrote:[color=blue]
> .oO(Purple Haze)
>
>[color=green]
>>ASP has <%Response.redirect("http://blahblah")%>
>>
>>What is the equivalent command in php to redirect a user to a new URL in
>>the same window? Or is there a Javascript command that does this job better?[/color]
>
>
> http://www.php.net/header
>
> Micha[/color]

After using:

header("Location: http://www.example.com/");

Its complaining headers have already been sent, and doesn't redirect.
Agelmar
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Redirect


Purple Haze wrote:[color=blue]
> Michael Fesser wrote:[color=green]
>> .oO(Purple Haze)
>>
>>[color=darkred]
>>> ASP has <%Response.redirect("http://blahblah")%>
>>>
>>> What is the equivalent command in php to redirect a user to a new
>>> URL in the same window? Or is there a Javascript command that does
>>> this job better?[/color]
>>
>>
>> http://www.php.net/header
>>
>> Micha[/color]
>
> After using:
>
> header("Location: http://www.example.com/");
>
> Its complaining headers have already been sent, and doesn't redirect.[/color]

Read the entire section on the header() function, don't just skip down to
the examples. header() must be called before any output (including
whitespace before <?php) has been sent.


Purple Haze
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Redirect


Agelmar wrote:
[color=blue]
> Purple Haze wrote:
>[color=green]
>>Michael Fesser wrote:
>>[color=darkred]
>>> .oO(Purple Haze)
>>>
>>>
>>>
>>>>ASP has <%Response.redirect("http://blahblah")%>
>>>>
>>>>What is the equivalent command in php to redirect a user to a new
>>>>URL in the same window? Or is there a Javascript command that does
>>>>this job better?
>>>
>>>
>>>http://www.php.net/header
>>>
>>>Micha[/color]
>>
>>After using:
>>
>>header("Location: http://www.example.com/");
>>
>>Its complaining headers have already been sent, and doesn't redirect.[/color]
>
>
> Read the entire section on the header() function, don't just skip down to
> the examples. header() must be called before any output (including
> whitespace before <?php) has been sent.
>
>[/color]

I guess I should have been more specfic, I need a redirect-function that
can be called anytime in the script. In this instance after the script
has determined that a vaild user and pass have been submitted.

Purple Haze
Michael Fesser
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Redirect


.oO(Purple Haze)
[color=blue]
>I guess I should have been more specfic, I need a redirect-function that
>can be called anytime in the script. In this instance after the script
>has determined that a vaild user and pass have been submitted.[/color]

Of course you can do this with header(), but you either

* have to make sure there's no other output sent to the browser before
(Does the check for a valid user prints anything out?)

or

* have to use output buffering.

Micha
Purple Haze
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Redirect


Michael Fesser wrote:
[color=blue]
> .oO(Purple Haze)
>
>[color=green]
>>I guess I should have been more specfic, I need a redirect-function that
>>can be called anytime in the script. In this instance after the script
>>has determined that a vaild user and pass have been submitted.[/color]
>
>
> Of course you can do this with header(), but you either
>
> * have to make sure there's no other output sent to the browser before
> (Does the check for a valid user prints anything out?)
>
> or
>
> * have to use output buffering.
>
> Micha[/color]

I am still unclear what your referring to, perhaps a copy of the script
will help...

<center>
<form ACTION="login.php" name="saveform" METHOD="POST" align="center">
<p><input NAME="username"VALUE SIZE="8" MAXLENGTH="16" tabindex="1"></p>
<p><input type="password"name="password" size="8" tabindex="2"
maxlength="8"></p>
<p><input TYPE="button"NAME="FormsButton2" VALUE="Validate"
ONCLICK="submit()" tabindex="3"style="font-family: Verdana; font-size:
12pt">
</form>
</center>

<?php
$mysql_database="login";
$mysql_username="root";
$mysql_password="";
$link = mysql_connect("localhost",$mysql_username,$mysql_p assword) or
die ("Unable to connect to SQL server");
mysql_select_db($mysql_database,$link) or die ("Unable to select database");
$username = $HTTP_POST_VARS['username'];
$password = $HTTP_POST_VARS['password'];

if (isset($password))
{
$qstr = "SELECT * from members where user ='$username' and pass
='$password'";
$result = mysql_query($qstr);

if (mysql_num_rows($result))
{
echo "<font color=#008000><Center><b>**Successful
Login**</b></Center></font>";
header("Location: http://www.example.com/");
exit;
}
else echo "<font color=#ff0000><Center><b>**Failed
Login**</b></Center></font>";
mysql_close();

}
else echo "<font color=#ff8000><Center><b>**No login
attempted**</b></Center></font>";
?>
Purple Haze
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Redirect


Purple Haze wrote:
[color=blue]
> Michael Fesser wrote:
>[color=green]
>> .oO(Purple Haze)
>>
>>[color=darkred]
>>> I guess I should have been more specfic, I need a redirect-function
>>> that can be called anytime in the script. In this instance after the
>>> script has determined that a vaild user and pass have been submitted.[/color]
>>
>>
>>
>> Of course you can do this with header(), but you either
>>
>> * have to make sure there's no other output sent to the browser before
>> (Does the check for a valid user prints anything out?)
>>
>> or
>>
>> * have to use output buffering.
>>
>> Micha[/color]
>
>[/color]

Found the answer to my own question :/

I had a feeling javascript was the key here:

echo "<script>window.location=\"login1.php\"</script>";

will do the job
Daniel Tryba
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Redirect


Purple Haze <xxx@xxx.com> wrote:[color=blue]
> I had a feeling javascript was the key here:
>
> echo "<script>window.location=\"login1.php\"</script>";
>
> will do the job[/color]

What about any client without javascript?

Fix your php code instead...

--

Daniel Tryba

Michael Fesser
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Redirect


.oO(Purple Haze)
[color=blue]
>Found the answer to my own question :/
>
>I had a feeling javascript was the key here:
>
>echo "<script>window.location=\"login1.php\"</script>";
>
>will do the job[/color]

Unreliable.

Try to fix your script instead: First - before anything else - check if
the user is valid, then redirect or print out the form.

Micha

PS: Please don't send me copies of your postings by e-mail. I read the
groups I'm posting to.
Chris Hope
Guest
 
Posts: n/a
#11: Jul 17 '05

re: Redirect


Purple Haze wrote:
[color=blue]
> Purple Haze wrote:
>[color=green]
>> Michael Fesser wrote:
>>[color=darkred]
>>> .oO(Purple Haze)
>>>
>>>
>>>> I guess I should have been more specfic, I need a redirect-function
>>>> that can be called anytime in the script. In this instance after the
>>>> script has determined that a vaild user and pass have been submitted.
>>>
>>>
>>>
>>> Of course you can do this with header(), but you either
>>>
>>> * have to make sure there's no other output sent to the browser before
>>> (Does the check for a valid user prints anything out?)
>>>
>>> or
>>>
>>> * have to use output buffering.
>>>
>>> Micha[/color]
>>
>>[/color]
>
> Found the answer to my own question :/
>
> I had a feeling javascript was the key here:
>
> echo "<script>window.location=\"login1.php\"</script>";
>
> will do the job[/color]

Bad idea. If they have Javascript disabled or Javascript blocking software
they aren't going to be redirected either.

You have two solutions:

1) Make the very first thing in the page BEFORE any HTML <?php
ob_start(); ?> which will start output buffering. You can then set any
headers you want at any point in the page and the HTML won't be sent to the
browser until the script ends.

2) Move all your HTML down to be underneath your PHP code and recode the
page as appropriate.

In my opinion, and I'm sure many others here, (2) is the better solution as
it makes sense for your validation to be done before rendering any HTML .


--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Guest
 
Posts: n/a
#12: Jul 17 '05

re: Redirect


> What is the equivalent command in php to redirect a user to a new URL in[color=blue]
> the same window? Or is there a Javascript command that does this job[/color]
better?

From reading your posts and the others responses, I have determined that you
almost have it.
FYI, do not use the javascript method -- that will not always work.

Your code is fine; what you need to do is, put your PHP code ABOVE your HTML
code.
In your PHP code, after you issue the header() command, you need to add
exit(); to the code
so that nothing else on the page executes once you redirect the browser's
location.

If the user's login failed, the html form will show up since it is below the
PHP code.

Hope this helps -

____________________________________
Wil Moore III, MCP | Integrations Specialist


Purple Haze
Guest
 
Posts: n/a
#13: Jul 17 '05

re: Redirect


laidbak69@hotmail.com wrote:
[color=blue][color=green]
>>What is the equivalent command in php to redirect a user to a new URL in
>>the same window? Or is there a Javascript command that does this job[/color]
>
> better?
>
> From reading your posts and the others responses, I have determined that you
> almost have it.
> FYI, do not use the javascript method -- that will not always work.
>
> Your code is fine; what you need to do is, put your PHP code ABOVE your HTML
> code.
> In your PHP code, after you issue the header() command, you need to add
> exit(); to the code
> so that nothing else on the page executes once you redirect the browser's
> location.
>
> If the user's login failed, the html form will show up since it is below the
> PHP code.
>
> Hope this helps -
>
> ____________________________________
> Wil Moore III, MCP | Integrations Specialist
>
>[/color]
Yep, I was able to accomplish that using a previous post, Thanks anyway.

I can also see why using header() is better than javascript.
Tony Marston
Guest
 
Posts: n/a
#14: Jul 17 '05

re: Redirect



"Purple Haze" <xxx@xxx.com> wrote in message
news:MuI4d.741$%06.624@newsread3.news.atl.earthlin k.net...[color=blue]
> Michael Fesser wrote:
>[color=green]
>> .oO(Purple Haze)
>>
>>[color=darkred]
>>>I guess I should have been more specfic, I need a redirect-function that
>>>can be called anytime in the script. In this instance after the script
>>>has determined that a vaild user and pass have been submitted.[/color]
>>
>>
>> Of course you can do this with header(), but you either
>>
>> * have to make sure there's no other output sent to the browser before
>> (Does the check for a valid user prints anything out?)
>>
>> or
>>
>> * have to use output buffering.
>>
>> Micha[/color]
>
> I am still unclear what your referring to, perhaps a copy of the script
> will help...
>
> <center>
> <form ACTION="login.php" name="saveform" METHOD="POST" align="center">
> <p><input NAME="username"VALUE SIZE="8" MAXLENGTH="16" tabindex="1"></p>
> <p><input type="password"name="password" size="8" tabindex="2"
> maxlength="8"></p>
> <p><input TYPE="button"NAME="FormsButton2" VALUE="Validate"
> ONCLICK="submit()" tabindex="3"style="font-family: Verdana; font-size:
> 12pt">
> </form>
> </center>
>
> <?php
> $mysql_database="login";
> $mysql_username="root";
> $mysql_password="";
> $link = mysql_connect("localhost",$mysql_username,$mysql_p assword) or die
> ("Unable to connect to SQL server");
> mysql_select_db($mysql_database,$link) or die ("Unable to select
> database");
> $username = $HTTP_POST_VARS['username'];
> $password = $HTTP_POST_VARS['password'];
>
> if (isset($password))
> {
> $qstr = "SELECT * from members where user ='$username' and pass
> ='$password'";
> $result = mysql_query($qstr);
>
> if (mysql_num_rows($result))
> {
> echo "<font color=#008000><Center><b>**Successful
> Login**</b></Center></font>";
> header("Location: http://www.example.com/");
> exit;
> }[/color]

There'e your problem right there - you have an echo statement just before
the header(). Remove the echo and the header() will work.

--
Tony Marston

http://www.tonymarston.net


[color=blue]
> else echo "<font color=#ff0000><Center><b>**Failed
> Login**</b></Center></font>";
> mysql_close();
>
> }
> else echo "<font color=#ff8000><Center><b>**No login
> attempted**</b></Center></font>";
> ?>[/color]


Closed Thread