Welcome to WebmasterWorld Guest from 34.237.76.249

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP "goto" (equivalent of <jsp:forward>)?

How to transfer control to another .php file without header() overhead

     
7:32 am on Feb 16, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 30, 2002
posts:11
votes: 0


I like the idea of keeping my form definition and processing code in the same file (by using <POST action="$PHP_SELF" >. But I'm trying to figure out how to transfer control to the next page based on the result of the forms processing.

In JSP, i would use <jsp:forward>, which is basically a "go to" command. The only reference I can find to "transfer of control" in PHP is the header(location: ) function. But that requires a complete server/browser/server interchange.

There must be a more direct way to transfer control in PHP. Would someone please tell me what it is?

8:48 am on Feb 16, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:July 13, 2002
posts:133
votes: 0


You could always do:
include('2ndfile.php');
exit();
11:59 am on Feb 16, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


As c3oc3o wrote there´s [url=http://www.php.net/require]require() [php.net][/url], [url=http://www.php.net/require_once]require_once() [php.net][/url], [url=http://www.php.net/include]include() [php.net][/url], and [url=http://www.php.net/include_once]include_once() [php.net][/url]. When used with local files these statements do not cause any HTTP communication. All these statements will return control to the including file after the included file has been processed. Use exit() [php.net] either in the included file or right after the [url=http://www.php.net/include]include() [php.net][/url] statement.

Andreas

8:53 pm on Feb 16, 2003 (gmt 0)

New User

10+ Year Member

joined:Dec 30, 2002
posts:11
votes: 0


Thank you, thank you, thank you! I knew there was a better way than header(). I had been looking in the PHP manual under "Control Structures," where there is no mention of exit(), only "return." And, since die() is an alias for exit(), I assumed it was for exceptional processing only.

FYI, I just posted the following to the PHP site for consideration for inclusion in the manual under exit(). What do you think?:

exit -- Output a message and terminate all script execution for this request. (For HTTP requests, this causes the response generated thus far to be sent to the browser.)

Use "return" to terminate the current script only and return to any script that included it (resuming execution at the point after the include).

"exit" can be used immediately following a "require" or "include" to simulate unconditional branching (i.e. "go to") to another script file.

[End of submission]

9:14 am on Feb 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


Regarding your description of the exit command, this is only true if output buffering is turned on. When output buffering is off, the output of the script is sent to the user 'on the fly'.

Return is generally used for ending functions. I think it's ability to act like exit is just a fallback.

Although an alias, die is generally used when an error occurs. A common example would be:
$result = mysql_query ($command, $link_id) or die ("ERROR: Mysql Query failed". mysql_error());

HTH

Allen