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

erogol
  • 13,156
  • 33
  • 101
  • 155

7 Answers7

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.

Tanner Ottinger
  • 2,970
  • 4
  • 22
  • 28
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"
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 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. – user124384 Jul 23 '15 at 16:20
  • That's how it is. Without session_start(); you can't read the session variables. – Michael Rogers Oct 22 '17 at 19:50
3

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

Albireo
  • 10,977
  • 13
  • 62
  • 96
  • Do all the variables get passed across when you include a script, or just globals? – Chris Kimpton Jul 13 '11 at 04:57
  • 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. – Albireo Jul 13 '11 at 07:24
  • Thanks - is that different from require ? I had tried require and did not seem to do that. – Chris Kimpton Jul 14 '11 at 09:10
  • While if `include` can't find the specified file it will raise a warning, `require` will throw a fatal exception. – Albireo Jul 14 '11 at 10:16
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
Paul Siersma
  • 2,036
  • 1
  • 22
  • 25
Dan Blows
  • 20,846
  • 10
  • 65
  • 96
  • 1
    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. – Pavel Oct 09 '20 at 00:26
0

You can use:

kenorb
  • 155,785
  • 88
  • 678
  • 743
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;
0

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

dotslashlu
  • 3,361
  • 4
  • 29
  • 56