Call one script from another ?

Discussion in 'PHP' started by chriseccles2, Oct 19, 2008.

  1. #1
    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
     
    chriseccles2, Oct 19, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    why not doing some if-else statements.?
    can you explain more? are you talking about functions or classes?
     
    bartolay13, Oct 20, 2008 IP
  3. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Shoro, Oct 20, 2008 IP
  4. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    chriseccles2, Oct 20, 2008 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    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.
     
    bartolay13, Oct 20, 2008 IP
  6. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    -
    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 ! :confused:

    -
    Chris
     
    chriseccles2, Oct 20, 2008 IP
  7. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #7
    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
    }

    ?????
     
    bartolay13, Oct 20, 2008 IP
  8. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    :D

    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
     
    chriseccles2, Oct 20, 2008 IP
  9. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #9
    
    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:
     
    blueparukia, Oct 20, 2008 IP
  10. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    chriseccles2, Oct 20, 2008 IP
  11. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #11
    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
     
    bartolay13, Oct 20, 2008 IP
  12. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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
     
    chriseccles2, Oct 20, 2008 IP
  13. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #13
    can you post the "new program"
     
    bartolay13, Oct 20, 2008 IP
  14. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    <?php

    echo "THIS PROGRAM WAS CALLED SUCCESSFULLY";

    die();


    ?>


    :eek:

    Almost beats "Hello, World" for fundamental simplicity .....

    -
    Chris
     
    chriseccles2, Oct 20, 2008 IP
  15. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #15
    use include inside the if-else statment and remove the die()
     
    bartolay13, Oct 20, 2008 IP
  16. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #16
    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
     
    ads2help, Oct 20, 2008 IP
  17. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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 .......... :D


    -
    Chris
     
    chriseccles2, Oct 20, 2008 IP
  18. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #18
    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?
     
    deathshadow, Oct 20, 2008 IP
  19. chriseccles2

    chriseccles2 Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    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
     
    chriseccles2, Oct 20, 2008 IP