Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,906 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,771 people online right now. Registration is fast and FREE... Join Now!




redirecting to another page

 
Reply to this topicStart new topic

redirecting to another page

samn84
24 Mar, 2008 - 03:55 AM
Post #1

New D.I.C Head
*

Joined: 21 Feb, 2008
Posts: 41

how do we redirect to another page after a little code has been executed and not by using this method:

CODE

header(location:"url");


which runs before any other thing in the page is executed.......

i want a php function for redirecting a page becoz my url has php variables in it.
User is offlineProfile CardPM
+Quote Post

spearfish
RE: Redirecting To Another Page
24 Mar, 2008 - 07:53 AM
Post #2

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
That's exactly how you do it. The PHP executes in a top-to-bottom fashion. Meaning, any code before the redirect will still happen. However, you cannot have any HTML output before a redirect, e.g.,
CODE

echo "My name is Fred";
header( ' Location: http://www.dreamincode.net ');

is illegal. That's because of how HTTP works. The header information is just that. The echoed string falls under the body of the document, and you cannot modify the header information after the body information has been sent (somebody correct me if I'm wrong).

Regardless, this:
CODE

$name = $_GET['thename'];
mysql_query("insert into mytable (name) values ('$name') ");
header("Location: http://www.hotmail.com");

is still valid and will insert data into a MySQL table before the redirect (provided my syntax is right for the insert statement wink2.gif )
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:50PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month