JustinK101 Posted September 16, 2006 Share Posted September 16, 2006 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. Link to comment https://forums.phpfreaks.com/topic/20947-can-php-take-the-place-of-javascript-page-redirects/ Share on other sites More sharing options...
trillion Posted September 16, 2006 Share Posted September 16, 2006 <?phpheader('Location: path/to/file.php');?>this has to be placed before any visible content Link to comment https://forums.phpfreaks.com/topic/20947-can-php-take-the-place-of-javascript-page-redirects/#findComment-92840 Share on other sites More sharing options...
mainewoods Posted September 16, 2006 Share Posted September 16, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/20947-can-php-take-the-place-of-javascript-page-redirects/#findComment-92841 Share on other sites More sharing options...
JustinK101 Posted September 16, 2006 Author Share Posted September 16, 2006 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';} Link to comment https://forums.phpfreaks.com/topic/20947-can-php-take-the-place-of-javascript-page-redirects/#findComment-92842 Share on other sites More sharing options...
michaellunsford Posted September 16, 2006 Share Posted September 16, 2006 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. Link to comment https://forums.phpfreaks.com/topic/20947-can-php-take-the-place-of-javascript-page-redirects/#findComment-92853 Share on other sites More sharing options...
JustinK101 Posted September 16, 2006 Author Share Posted September 16, 2006 :) 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. Link to comment https://forums.phpfreaks.com/topic/20947-can-php-take-the-place-of-javascript-page-redirects/#findComment-92861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.