6 Replies - 286 Views - Last Post: 21 April 2013 - 01:43 PM Rate Topic: -----

#1 msz_900  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 09-February 11

php header error

Posted 21 April 2013 - 11:32 AM

hy to every one ...
i tried many times but can't understand one thing...

echo " redirect to main page";
header("location:index.php");


in the above code i want that when it redirect's then it will show the message
 echo"redirect to main page";

but it will not why?
it will only redirect but can't show any mesage.
any one tell me why?
is it the hidden error in redirection?

Is This A Good Question/Topic? 0
  • +

Replies To: php header error

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: php header error

Posted 21 April 2013 - 11:51 AM

What makes you jump to the conclusion that it is an error? :dontgetit:

It might display the message, but immediately redirects to a different location, usually too quickly for anyone to read the message.

There are ways to pause before the redirect, but much simpler is just to remove the message. Alternatively, there are ways for index.php to decide that the user was re-directed to this page, and display a suitable message - "Re-directed here!".

This will probably work:

header('Refresh: 5; URL=http://yoursite.com/index.php'); // or
header('Refresh: 5; URL=index.php');

but I believe it's use is discouraged.

This post has been edited by andrewsw: 21 April 2013 - 11:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 msz_900  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 09-February 11

Re: php header error

Posted 21 April 2013 - 11:59 AM

sory bro!
i couldn't understand...
have any way to clear it? :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: php header error

Posted 21 April 2013 - 12:11 PM

Yeah, no. Sorry bro. Try re-reading my previous post.

This post has been edited by Dormilich: 21 April 2013 - 11:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,764
  • Joined: 08-June 10

Re: php header error

Posted 21 April 2013 - 01:26 PM

View Postmsz_900, on 21 April 2013 - 06:32 PM, said:

.
i tried many times but can't understand one thing...

echo " redirect to main page";
header("location:index.php");


in the above code i want that when it redirect's then it will show the message
 echo"redirect to main page";

but it will not why?


From the PHP manual entry on the header() function:

php.net said:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

HTTP communications are split into headers and the body. You can't change the headers once you start adding to the body. Everything you print, and every client-side content outside PHP blocks, will be added to the body. (You can bypass that issue using Output Buffering. It should be avoided when possible though.)

Therefore, printing a message and then setting a location header will not work. It has to be the other way around. Of course, odds are that if you set the location header that the browser will redirect you before showing the content. If you want a delayed redirect, use the Refresh header andrewsw demonstrated.
Was This Post Helpful? 2
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: php header error

Posted 21 April 2013 - 01:36 PM

@Atli Oops! I must be tired :whistling:
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: php header error

Posted 21 April 2013 - 01:43 PM

@Atli Just discovered you (possibly..) giving similar good advice a few years ago :)

http://www.daniweb.c...-with-a-message

This post has been edited by andrewsw: 21 April 2013 - 01:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1