This may seem daft, and I've not needed to explore this until now, but is there an easy way to call one PHP script from within another, allowing the calling script to die(); gracefully ? - Chris
why not doing some if-else statements.? can you explain more? are you talking about functions or classes?
Using exec to run it from the command line (i.e. exec('php thescript.php')) should work. If you don't want to do that, doing something like using file_get_contents to access the script using the external url to the script might also work.
Thanks, lads. - OK. My fault for not describing the requirement properly: Let's say I have a script which is validating a user's LOGIN and PW. If they are bad, I display the login page again with the error message. No problem. User then just has another try at logging in. - What I need is to jump to a different PHP script if the user's LOGIN and PW are OK. - Like: <?php if (($userName != $nameOnFile) or ($pw != $pwOnFile)) { .... func to generate error page .... } else { .... jump to another script here .... } ?> Kind of thing .... - Chris
if-else is good you can also try switch, well it really depends on you then include("otherfile.php"); or require("otherfile.php"); or require_once("otherfile.php"); thats it... well i just thought your using any classes, functions or objects.
- No, bartolay13. Everything needed would have been previously stored in my custom session file. - Just need to know how to do that simple jump: Calling script stops and passes execution to another named script. Just can't see how to do it ! - Chris
from what i understand is that when your username and password == true then execute a script from another file.. ofcourse you store your variables to your session variable i just cant get it, its like just if(isset($_SESSION['name']) || $_SESSION['name']!= "") { execute function.. or include("page.php"); or redirect ("page.php"); }else { error message that shows your username and password is not true } ?????
That would be cool, bartolay13, if only there were actually a PHP function named redirect(); ! ! ! ! ! ! That is what I need. How do I do a "redirect" ? - Chris
header('Location: http://site.com'); PHP: Or to get fancy, if you were to use it a lot: function redirect($page){ header('Location: '. $page); } redirect('http://site.com'); PHP:
Tried that before ! - Doesn't work. - I have a calling script ( calling.php ) which just contains: <?php echo "COMPILES FINE"; $s = header('Location: full_http_path_to_my_site/php/called.php'); die(); ?> and another ( called.php ) which is simply: <?php echo "THIS PROGRAM CALLED SUCCESSFULLY"; ?> - No result. The first program runs (of course !), but the called program doesn't get called. - Chris
try this one sir function redirect($time, $topage) { echo "<meta http-equiv=\"refresh\" content=\"{$time}; url={$topage}\" /> "; } $time = delay time of the redirection $topage = page
Parses fine, but still doesn't call the new program ! - This problem might require more aspirin and coffee than I first imagined it would ............. - Chris
<?php echo "THIS PROGRAM WAS CALLED SUCCESSFULLY"; die(); ?> Almost beats "Hello, World" for fundamental simplicity ..... - Chris
Try this: <?php ob_start(); echo "COMPILES FINE"; header('Location: called.php'); PHP: You output something before header() function which DOES NOT ALLOW ANY characters including whitespace b4 it. So you can either put ob_start() just after the line <?php and continue with your script (as in above) or put the header() just after the line <?php -------- And most probably u have your error reporting turned off? This is my guess
Proof that co-operative forum consultation yields results ! - bartolay13's suggestion to place an include() function inside a conditional block appears to work in the way I wished, although appears actually not to pass control to the included program, as such. [The original calling routine's URL remains in the browser URL line.] - The calling program is now: <?php if ($dummy == NULL) { echo "DO WE SEE ANY CODE EXECUTED HERE ?"; ?> <br> <br> <br> <br> <?php include("./called.php"); echo "THIS LINE SHOULD NOT BE EXECUTED AFTER THE INCLUDE CALL"; die(); } else { echo "FELL THROUGH CONDITION"; die(); } ?> and the called program is: <?php echo "NO WE DO NOT"; ?> <br> <br> <?php echo "THIS PROGRAM WAS CALLED SUCCESSFULLY"; ?> <br> <br> <br> <br> <?php echo "THE OLD PROGRAM DIED GRACEFULLY"; die(); ?> This produces: DO WE SEE ANY CODE EXECUTED HERE ? NO WE DO NOT THIS PROGRAM WAS CALLED SUCCESSFULLY THE OLD PROGRAM DIED GRACEFULLY ... on screen, which shows that the line: echo "THIS LINE SHOULD NOT BE EXECUTED AFTER THE INCLUDE CALL"; is NOT being executed, and that the calling program in fact "appears" to suspend execution even BEFORE its die(); statement, which is the required behaviour. All of which begs the question: Is the calling program also still running on the server, and subject only to server timeout ? Such mad complexities for a Monday morning .......... - Chris
Ok, I see your 'issue' here. What you are failing to grasp is that the called file is NOT run as a separate program. When you include it you are making it part of the SAME program. THIS: called.php <?php die('aborting'); ?> Code (markup): calling.php <?php echo 'start<br />'; include('called.php'); echo 'end<br />'; ?> Code (markup): is the EXACT SAME THING as if you typed it as this: <?php echo 'start<br />'; die('aborting'); echo 'end<br />'; ?> Code (markup): It's all one program - not two separate ones! Using an include just 'inserts' that file into the one you are running as if it were part of the same program. For subfunctions like a user login, I HIGHLY recommend you compartmentalize it into a function. Includes that the user should not be calling directly (like a login.php) should NOT return any output on their own, and instead should be built as a function library. For example: login.library.php <?php function login($username) { echo ' Attempting Login '; return $userName=='jimbo'; ?> Code (markup): login.php <?php require_once(library.php); echo 'attempting login<br />'; if (login($_POST['username'])) { echo 'login ok'; do your logged in stuff here } else { echo 'login failed' } ?> Code (markup): See how that works?
Yeah, I see your point, deathshadow. - I got around to reasoning that the program ran as one entity, and I fully appreciate the login security issue you raised. - Preventing the served page source showing anything sensitive to the user is the easy bit ! I just, originally, couldn't figure the basic mechanism for "calling" another script, hence my original post. - Many thanks to all who responded. Shared wisdom - a great thing. - What I have now coded gives me the functionality I wanted, making individual PHP scripts smaller and, hence, faster. - Chris