Re: Redirect command in PHP ???
| From: | George Whiffen | Date: | Tue, 06 May 2003 13:04:20 +0000 |
| Subject: | Re: Redirect command in PHP ??? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-146508@lists.php.net to get a copy of this message | ||
Andrei,
Why is header() a problem?
If it is just that you cannot use it after having sent output then there are two solutions:
1. The proper solution
Change your code so that you check if you want a "redirect" before generating any output. In general you should only generate actual "output" at the end of your php script after executing any logic not directly associated with output. In particular anything that could cause an error or a change in the page you want to go should be handled before worrying about to output. This separation of processing and representation, (output), should make your php much easier to understand and change.
2. The quick fix
If you switch output buffering on, then the restriction on redirecting before generating output does not apply. See ob_start() in the PHP manual.
Hope this helps,
George
Andrei Verovski wrote:
Hi, Chris, I found "redirect" function in moregroupware. Here it is. Actually it is wrapper to header(). You were right. function redirect($uri) {if(PHP_OS=="WINNT" || PHP_OS=="WIN32") {// we have problems with REFRESH with opera, so we use // location for opera and refresh for IE on WIN32 if(ereg("Opera", $_SERVER["HTTP_USER_AGENT"], $args)) { header("Location: $uri"); exit(); } else { header("Refresh: 0; url=\"$uri\""); exit(); }} else { header("Location: $uri"); exit(); }} On Monday, May 5, 2003, at 03:56 PM, Chris Hayes wrote:Is there any kind of command (or script) in PHP equivalent to HTML "redirect"??? I am need to switch execution from one script to another WITHOUT include or require.On Monday, May 5, 2003, at 02:14 PM, Chris Hayes wrote: Header("Location: "http://www.site.org/index.php?module=x&func=y&id=15");Thanks for reply. However, is it possible to do it without Header(...)? The reason I would like to use redirect script (without include, require or header()) is that I have to switch pages and frames, called via Smarty template engine and scattered across large number of directories. include/require just mess up pathnames, and header() is also not appropriate in this case. I have seen such redirect() script (or function) in moregroupware, but there is enormous number of scripts, it is very hard to find out how they have done it.include and require do not mess up pathnames, it's just that when you use a relative path, the path they include will depend on which php script you started from. e.g. if path1/file1.php includes path2/file2.php, then if file2.php includes /x/file3.php, they will look insise path1 and not inside path2, as file1.php was the first one. build a complete path to be included to solve this problem. The easiest way i can think of to be sure to get the right path is to chop the current filename off of __FILE__. Otherwise you can choose from * javascript solutions. (here again be sure to use the complete url in location.href so netscape works too) * write a quick page form and include the meta tag you did not lke anyway * GET urls -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php