Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables between php scripts?

Is there any way to pass values and variables between php scripts?

Formally, I tried to code a login page and when user enter wrong input first another script will check the input and if it is wrong, site returns to the last script page and show a warning like "It is wrong input". For this aim, I need to pass values from scripts I guess.

Regards... :P

like image 950
erogol Avatar asked Apr 15 '11 14:04

erogol


3 Answers

You should look into session variables. This involves storing data on the server linked to a particular reference number (the "session id") which is then sent by the browser on each request (generally as a cookie). The server can see that the same user is accessing the page, and it sets the $_SESSION superglobal to reflect this.

For instance:

a.php

session_start(); // must be called before data is sent

$_SESSION['error_msg'] = 'Invalid input';

// redirect to b.php

b.php

<?php

session_start();

echo $_SESSION['error_msg']; // outputs "Invalid input"
like image 149
lonesomeday Avatar answered Nov 09 '22 09:11

lonesomeday


To pass info via GET:

    header('Location: otherScript.php?var1=val1&var2=val2');

Session:

    // first script
    session_start(); 
    $_SESSION['varName'] = 'varVal';
    header('Location: second_script.php'); // go to other

    // second script
    session_start(); 
    $myVar = $_SESSION['varName'];

Post: Take a look at this.

like image 24
Tanner Ottinger Avatar answered Nov 09 '22 09:11

Tanner Ottinger


Can't you include (or include_once or require) the other script?

like image 3
Albireo Avatar answered Nov 09 '22 10:11

Albireo