English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I would like a page to load when a function is called

function redirect()
{
print("redirecting..."); //this line prints on page1
header("location: http://www.yahoo.com/"); //but page1 doesnt change to Yahoo
exit;
}


Please help.

2006-07-07 07:53:42 · 5 answers · asked by Ozone 4 in Computers & Internet Programming & Design

Thanks for the answers. But think login in page.
Page1 loads, user fills form, click button, function is called.

How to I get page1 to go to different URL based on what the form is passing.

I guess this isnt really a redirect.

2006-07-07 08:05:22 · update #1

OK. I must not be phrasing this right.


I just started learning PHP on monday, so bearwith me if I'm oblivious.

I am creating a login page. When the user logs in, I want the username pulled from the form and sent to a function that appends it to the URL e.g. www.mysite.com?username.

I only added the "print("redirecting...");" line to test if the function was being called at all. I just need to construct a URL fromuser input and the load that URL.

Thanks everyone.

2006-07-07 08:18:47 · update #2

5 answers

The HTTP specification doesn't allow for any content before sending the header information. Therefore, your:

print("redirecting...");

...is being rejected by the HTTP server. Remove that and it will work. If your specs require you to show that, then I would suggest a JavaScript applet that directs the browser to go to the new url. The problem with this is, many browsers may have JavaScript turned off.

Thus it's just easier to use the Location: header.

--------------------------

(Edit based on additional details):

OK -- this will depend upon how you're processing the form. If, for example, you use a textbox to fill in a GET URL parameter (which is generally bad practice, BTW) then you can use something like:

$fname = $_GET['fname']; // First Name

header("Location: http://www.website.url/proc.php?n=$fname");

Perhaps if you elaborate on how you're processing the data (IE, database functions, case statement, etc) I could be more helpful.

----------------

Secondary edit:

OK -- sounds like just what I put above. Try that code I put up there, just substitute the form fields in your $_GET variables.

2006-07-07 08:05:36 · answer #1 · answered by Brandon F 3 · 0 0

The header redirect has to be the first thing sent. Thus, printing "redirecting" is breaking your script.

If it's a permanent redirect (you've renamed a page) you should instead use a 301:
http://www.webconfs.com/how-to-redirect-a-webpage.php

Mega

=======================


Based on the additional information that you gave, you just need to use a different php page in the html form's action attribute.


...


Then use login.php to process the login. Once you've checked the user, login.php can redirect (either to the "success" or the "try again" page) with the header(...) call.

Mega

2006-07-07 14:57:53 · answer #2 · answered by MegaAx0 2 · 0 0

You need to make sure that any redirects happen before anything else is sent to the browser (remove the print(... )). Headers like redirects and cookies can only be sent before anything else gets printed.

2006-07-07 15:06:57 · answer #3 · answered by John J 6 · 0 0

hmm... the code you wrote is ok, but still u have to consider the fact that headers should be sent before any single character is output by the script, this includes:
1. strings written with: print, echo
2. HTML code or even white space occuring before the
but you can still get around this constraint by using an output buffer: ( ob_start, ob_end ... these are standard php )

look at: http://www.php.net/ob_start

2006-07-07 15:41:36 · answer #4 · answered by shadyzay 1 · 0 0

Not sure how you are getting the link info from your users, but for example. If I pull the link infor from the user using post, you can do something like below.

if ($_POST[location] == ""){
header("Location: redirect.html");
}else {
header( "Location: $_POST[location]");
}
?>

There are many ways to do it. But it depends on how you are getting the link to begin with.

This is the most basic way of doing it and this is straight from the PHP manual:

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

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

Hope this helps. If not, try posting on http://prydain.futurecis.com/Forums

2006-07-07 15:01:12 · answer #5 · answered by William 2 · 0 0

fedest.com, questions and answers