Jump to content


Photo

Alternative to header redirect

php header alternative

  • Please log in to reply
14 replies to this topic

#1 alena1347

alena1347

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 05 February 2013 - 07:36 AM

I have a php file and need to redirect to other pages from middle of the page as header() needs to be put on the top is there any other way please.

#2 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 05 February 2013 - 08:05 AM

There are hacks that work around this, but they are of very varying quality and thus not recommended to use.

What you really should do is to restructure your site. Put all of the PHP code that processes data at the top of the page, and don't send out anything to the client until you've parsed all of the PHP code. Doing it this was removes a lot of artificial limits that you've imposed on the code, by mixing HTML and PHP, and it will make your life a lot easier in the future.
Keeping it simple.

#3 cutee4

cutee4

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 12 February 2013 - 07:02 AM

There may be any bug or error in site. According to me, make changes in code or change the site pages.. Or reinstruct.

#4 alena1347

alena1347

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 12 February 2013 - 07:13 AM

There may be any bug or error in site. According to me, make changes in code or change the site pages.. Or reinstruct.

It's not that, the header must be put before any output and I need to redirect after some output, I am currently using javascript window.location but need something else in php

#5 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,731 posts
  • LocationBonita, FL

Posted 12 February 2013 - 08:00 AM

... but need something else in php


There is nothing else.  header() is how you do a redirect from within PHP.  Your options are either re-structure your code so you can use header() or continue using a workaround like JS.

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#6 spencerharry80

spencerharry80

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 14 February 2013 - 05:56 AM

I followed the procedure stated in the earlier reply, but now it is giving me a scripting error.

#7 alena1347

alena1347

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 17 February 2013 - 11:06 PM

I followed the procedure stated in the earlier reply, but now it is giving me a scripting error.

could you post the code a little bit so we can solve it

#8 QuickOldCar

QuickOldCar

    Advanced Member

  • Members
  • PipPipPip
  • 1,914 posts
  • LocationNorthEast Pennsylvania

Posted 26 February 2013 - 09:35 PM

You could use a meta refresh, can also place a die() or exit() after it

 

<meta http-equiv="refresh" content="0;URL='http://mysite.com/'">

DynaIndex.com
Dynamic search index of websites, links, feeds, title, description, keywords and thumbnail snapshot.

#9 trq

trq

    Advanced Member

  • Administrators
  • 31,031 posts
  • LocationSydney, Australia.

Posted 27 February 2013 - 05:09 AM

It's not that, the header must be put before any output and I need to redirect after some output,

 

That makes no sense. Why are you outputting anything if all you are going to do is try and redirect the user to another page?

 

You need to fix your logic.


http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#10 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 27 February 2013 - 06:42 AM

It should be noted, that you can (and probably should) sent the refresh header via the header () function in PHP. As hinted to by the meta-tag's name http-equiv:
header ("Refresh: 5; URL=<a href="http://mysite.com/">http://mysite.com/</a>");

Edited by Christian F., 27 February 2013 - 06:43 AM.

Keeping it simple.

#11 alena1347

alena1347

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 03 March 2013 - 11:13 PM

That makes no sense. Why are you outputting anything if all you are going to do is try and redirect the user to another page?

 

You need to fix your logic.

There are situations when you need to put the header after some output code in a page or within an if condition after some output or multiple headers in a piece of code for which this is required



#12 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 04 March 2013 - 02:59 AM

No, there are no such situations. At least not if you've written the code properly, with separated business logic and presentation logic.
The trick is that you do not send anything to the screen/browser, until everything has been processed by PHP. At which point any headers that needs to be sent has been sent a long time ago, and you know exactly what needs to be sent to the browser.

Templating engines utilize this logic separation, and as such it might be a good idea to study up on them. Should help you write even better code, even if you choose not to use one for a project. ;)
Keeping it simple.

#13 alena1347

alena1347

    Advanced Member

  • Members
  • PipPipPip
  • 44 posts

Posted 04 March 2013 - 03:38 AM

No, there are no such situations. At least not if you've written the code properly, with separated business logic and presentation logic.
The trick is that you do not send anything to the screen/browser, until everything has been processed by PHP. At which point any headers that needs to be sent has been sent a long time ago, and you know exactly what needs to be sent to the browser.

Templating engines utilize this logic separation, and as such it might be a good idea to study up on them. Should help you write even better code, even if you choose not to use one for a project. ;)

OK got that thank you!



#14 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 04 March 2013 - 01:43 PM

You're welcome. :)
Keeping it simple.

#15 KirkLogan

KirkLogan

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationMaryland

Posted 05 March 2013 - 06:35 PM

It may also be worth noting (and correct me if im wrong here)

 

You can enable output buffering, at which point PHP Header functions will not be limited to Pre-Content execution. 

 

Christian is right though, it is best practice to keep your redirects prior to page content.

 

Just thought id weigh in.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com