Please wait for 15 seconds...

Tuesday, 25 September 2012



It is a common problem and happens very often with newbie. When you try to PHP header redirect the page to somewhere else it shows a warning something like this

cannot modify header information - headers already sent by (output ....


And this kind of error become more painful when you have a project to fix something in that and you did not code it before, too bad.

But what if you redirect the page by some other method than PHP header redirect. I am going to explain you a method. It worked for me and I hope it also help you out.

Instead of header() redirect you can use top.location.href. Copy n paste below code in your PHP library or in your regular PHP page.

<?php
 function custom_header_redirect($url){
      echo '<script type="text/javascript" language="javascript">window.top.location.href="'.$url.'"</script>';
}

And call custom_header_redirect($url) wherever you are using header("location:$url").

$url = "http://www.google.com";
custom_header_redirect($url);

Note:

You must use full path URL not related.

Allowed:

$url = "http://www.mygrtsite.com/profile.php?name=someone";
custom_header_redirect($url);

Not Allowed:

$url = "profile.php?name=someone";
custom_header_redirect($url);

If there is any PHP header problem occurs still your page will be redirected where you mentioned in $url.

Posted by Atul Prasad Gupta

2 comments:

  1. I truly think this web page needs much more concern. I’ll probably be again to study much more on custom redirect function in php, thanks for that details.

    ReplyDelete

Subscribe Techsirius

Techsirius on Facebook