Jump to content


Photo

Can PHP Take The Place Of JavaScript Page Redirects?


  • Please log in to reply
5 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 16 September 2006 - 03:35 AM

Hello All,

I am wondering if you guys know of a way to do page redirects with purely php? Currently I do all my redirects with javascript:


   document.location = 'my_new_url';


I do redirects a lot, for example in scripts that preform a delete action of a record in the database. I usually have a link that points to a page of almost purely PHP, which after it does the mysql delete query it simply redirects page to the previous page. I do this a lot.

Firstly, is this bad technique? Do you guys find yourself doing a decent amount of page redirects?

Secondly, is there a solution in php that is easy to do redirects?

Thanks much.

#2 trillion

trillion
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 16 September 2006 - 03:43 AM

<?php
header('Location: path/to/file.php');
?>

this has to be placed before any visible content


#3 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 16 September 2006 - 03:44 AM

header("location: **specifiy full redirect url**");
-is a better choice than your usage of javascript because it doesn't depend on browser settings to work

#4 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 16 September 2006 - 03:46 AM

Yeah that's what I have seen before, but that solution doesnt work for me. It is no good. Often times I do If statements and conditionals and decide to do the redirect depending. Also, often I do work, such as update a database, or create a record in a database, and then want to redirect.

Example:
if(mysql_num_rows($result) > 0)
{
   echo 'DO JAVA SCRIPT REDIRECT CODE';
}
else
{
   echo 'DO JAVA SCRIPT REDIRECT CODE DIFFERENT PAGE';
}

#5 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 16 September 2006 - 04:22 AM

as long as you don't output anything to the browser before you use the
header
command, you're fine. Your example as written would work.

#6 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 16 September 2006 - 04:34 AM

:) LOL Ok my example did'nt show that there is generally HTML above that. Ok, I suppose I have my answer gotta use JavaScript. I understand why PHP can only interact with the header, server sided. Javascript still cant be beat for some features, such as this, and alert(), and some error checking.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users