14

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

1

7 Answers 7

24

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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"

2 Comments

This worked flawlessly, although I'm a little confused as to why you're supposed to use "session_start()" in b.php when it's more like "session_continue() since presumably it doesn't make a new session.
That's how it is. Without session_start(); you can't read the session variables.
3

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

4 Comments

Do all the variables get passed across when you include a script, or just globals?
When you include another script, its code gets "pasted" in the including one, so it gets the same things you would have writing the code directly in the first one.
Thanks - is that different from require ? I had tried require and did not seem to do that.
While if include can't find the specified file it will raise a warning, require will throw a fatal exception.
3

The quick way would be to use either global or session variables.

global $variable = 'something';

The 'better' way of doing it would be to include the script and pass the variable by parameter like

// script1.php contains function 'add3'
function add3( $value ) {
  return $value + 3;
}

// script2.php
include "script1.php";
echo 'Value is '.add3(2); // Value is 5

1 Comment

This one is my favourite. Sometimes you cannot use sessions, but this way you don't even have to. The only thing is you have to do global $variable; $variable = 'something'; so declaration and assignment are not in one line.
0

I would say that you could also store a variable in cache if you really need.

Comments

0

You can use:

Comments

0

I use extract() method to pass variable among PHP Scripts. It look like below example:

1. File index.php

<?php
$data = [
    'title'=>'hello',
    'content'=>'hello world'
];
extract($data);
require 'content.php';

2. File content.php :

<?php 
echo $title;
echo $content;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.