Php Redirect? Or load a new page?

N

Nonee

Hello all-

I have a php page that has a form box in it. With that form box, I
check and see if the value in the form + ".php" is a file that exists.
(No probs so far) Then if it does exist, I want to redirect the
browser to go there. I know there is a header("location: $filename");
that could work but it doesn't. What am I missing and how can I do
this?

Thanks VERY much... (Been racking my brain and the net all day)

-Josh
 
M

Mark Parnell

Previously in alt.html said:
I know there is a header("location: $filename");
that could work but it doesn't. What am I missing and how can I do
this?

Presumably you've already added .php to the filename.

header("location: ".$filename);

At the moment you're trying to send them to a file called "$filename",
which obviously doesn't exist. Short answer: don't quote the variable
name.
 
O

Oli Filth

Mark Parnell said the following on 08/09/2005 08:24:
Can you post a *short* sample of code that shows how you're handling
extracting the value from the form, and then how you're using the
header() statement?

NOTE: this would be better off in a PHP NG...
Presumably you've already added .php to the filename.

header("location: ".$filename);

At the moment you're trying to send them to a file called "$filename",
which obviously doesn't exist. Short answer: don't quote the variable
name.

Actually, in PHP, that's fine. With double-quoted strings, PHP performs
in-line variable substitution.
 
D

David Dorward

Nonee said:
I know there is a header("location: $filename"); that could work but it
doesn't.

No idea why, other then perhaps your browser being picky and insisting on an
absolute URL (as required by the HTTP spec) rather then the relative one
you are giving it. Most browsers are pretty good at error recovery along
those line though.

I'd probably start constructing HTTP requests by hand with a telnet client
to see exactly what was being sent back and forth.
 
B

boclair

David said:
Nonee wrote:




No idea why, other then perhaps your browser being picky and insisting on an
absolute URL (as required by the HTTP spec) rather then the relative one
you are giving it. Most browsers are pretty good at error recovery along
those line though.

I'd probably start constructing HTTP requests by hand with a telnet client
to see exactly what was being sent back and forth.
All that but the address must be absolute. See the php manual

Something like this perhaps

("Location: http://" .$_SERVER['HTTP_HOST']. "/" . $filename);

Louise
 
N

Nonee

<?php
if (empty($_POST["name"]))
{
}
else
{
$name=$_POST["name"];
$filename = $name."/index.php";
if (file_exists($filename))
{
echo "The file $filename does exist";
header("Location:
http://".$_SERVER['HTTP_HOST']."/".$filename);
}
else
{
echo "The file $filename does not exist";
}
}
?>

That is the code and it is still not working... Not sure as to why
though. Remember, someone types in a name in a form textbox and posts
it to the same php file. From the submit post, it posts the textbox
("name") and I retrieve it from the $_Post. Etc etc etc. The rest is
pretty self-explainitory. Any other ideas?

Thanks!!!


P.S. Btw, the $name."/index.php" points to a subdirectory using the
$name as the subdirectory. Confused? lol...
 
N

Nonee

<?php
if (empty($_POST["name"]))
{
}
else
{
$name=$_POST["name"];
$filename = $name."/index.php";
if (file_exists($filename))
{
echo "The file $filename does exist Location:
http://".$_SERVER['HTTP_HOST']."/".$filename;
header("Location:
http://".$_SERVER['HTTP_HOST']."/".$filename);
}
else
{
echo "The file $filename does not exist";
}
}
?>

Ok, an update. The new location is pointing to the right directory
but it is still not loading the new page. In addition, it is finding
the file. It just will NOT relocate to the new location. Grrr....
 
B

boclair

Nonee said:
<?php
if (empty($_POST["name"]))
{
}
else
{
$name=$_POST["name"];
$filename = $name."/index.php";
if (file_exists($filename))
{
echo "The file $filename does exist Location:
http://".$_SERVER['HTTP_HOST']."/".$filename;
header("Location:
http://".$_SERVER['HTTP_HOST']."/".$filename);
}
else
{
echo "The file $filename does not exist";
}
}
?>

Ok, an update. The new location is pointing to the right directory
but it is still not loading the new page. In addition, it is finding
the file. It just will NOT relocate to the new location. Grrr....

Do you have errors reported?
http://au3.php.net/error_reporting

Louise
 
T

Toby Inkster

Mark said:
header("location: ".$filename);

header("location: $filename") and header("location: ".$filename) are
exactly identical statements.

My two suggestions are:

1. When using your file_exists function, try using the full path from the
computer's root directory (which is probably different from the Apache
ServerRoot, unless you want to get rooted).

2. Make sure you're putting the header() statement before your PHP has
outputted any other data to the browser (including blank lines).
 
Joined
Nov 14, 2009
Messages
1
Reaction score
0
Hi there,
I tried to give you the solution on this message but for some odd reason all references to http are taken as links and I am not allowed to post links as I am a newcomer here. I am trying to attach it as a text file for you. Hope it work.

View attachment page redirect solution.txt
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top