Jump to content

Archived

This topic is now archived and is closed to further replies.

JustinK101

Can PHP Take The Place Of JavaScript Page Redirects?

Recommended Posts

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:

[code]

  document.location = 'my_new_url';

[/code]

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.

Share this post


Link to post
Share on other sites
[code]header("location: **specifiy full redirect url**");[/code]
-is a better choice than your usage of javascript because it doesn't depend on browser settings to work

Share this post


Link to post
Share on other sites
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';
}

Share this post


Link to post
Share on other sites
as long as you don't output anything to the browser before you use the [code=php:0]header[/code] command, you're fine. Your example as written would work.

Share this post


Link to post
Share on other sites
:) 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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.