-3

I'm stuck trying to pass variables between 2 PHP files a.php and b.php using require(). I learned that PHP variables have a single scope, which means that if a.php includes b.php, it can read its variables. However, I would like b.php to be able to read a variable from a.php with my current setup. For example, I can print out $msg if it is set in b.php but not a.php. Is there a simple way to do this?

a.php

<?php

$msg = "Welcome NEW USER ! We can't wait for you to check out our books!";
require("connect.php");
require("b.php");
$table = "SELECT * FROM `login`";
if ($query_run=mysqli_query($cnn,$table)){

$username = $_POST['username'];
$pw = $_POST['password'];

$sql = "INSERT INTO login (username,password) VALUES ('{$username}','{$pw}')";


if($cnn->query($sql)===TRUE){echo "Query ran successfully";}
else{echo "Error: ".$sql."<br>".$cnn->error;}
}
else{die("table connection failed");}
$cnn.close();

?>

b.php

<?php
echo "<h1>".$msg."</h1>
<form action='' method='post'>
Username: <input type='text' name='username'><br>
Password: <input type='text' name='password'><br>
<input type='submit' name='submit'>
</form>
"


?>
I Like
  • 1,711
  • 2
  • 27
  • 52
  • 1
    if a.php includes/requires b.php, all vars in b are available in a. Use require_once on b.php for a.php, and require_once on a.php for b.php – clearshot66 Jul 06 '17 at 17:40
  • 2
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jul 06 '17 at 18:08
  • 2
    **Never store plain text passwords!** Please use ***PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)*** to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). ***It is not necessary to [escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jul 06 '17 at 18:08

5 Answers5

-1

Using $_SESSION will allow you to pass data between different files.

in a.php

$_SESSION['myName'] = "John";

in b.php

echo $_SESSION['myName']
Rushikumar
  • 1,774
  • 5
  • 18
  • 28
  • You really should only use session to store variables *between sessions*, not between files. You would be better of using globals, not sessions (as they have extra side-effects), but neither is a good idea. – Conor Mancone Jul 06 '17 at 17:45
-1

I'm not sure what your trouble is but your example should work. This most definitely works:

a.php

<?php
$from_a = 'hey';
require "b.php";
print $from_b;

b.php

print $from_a;
$from_b = 'okay';

The above is perfectly valid PHP and will work as intended with no errors. A script which is required by another script has full access to its variables and vice-versa.

Conor Mancone
  • 1,940
  • 16
  • 22
-1

If you make require an infinite cycling occurs,try require_once in each file. But the best solution is to create a third file be it c.php and include both b.php and a.php in it

user10089632
  • 5,216
  • 1
  • 26
  • 34
-1

No need to use of session.

if PHP file is included in other PHP file then variables also accessible in included PHP file.

Example

<?php

$a = 1;
include 'filename.php';

?>

Then $a is accessible in filename.php. use echo in included file and test..

Let me know if you feel any issue.

Kamran Jabbar
  • 858
  • 7
  • 21
-1

"which means that if a.php includes b.php, it can read its variables."

I think the concept is slightly different. When you call the language construct require, it will read and include the specified file as if it were written in the calling file itself.

So this:

<?php

// File a.php
$somevar = 6;
require "b.php";

?>
<?php

// File b.php
echo $somevar;

?>

Is effectively the same as

<?php

// File a.php
$somevar = 6;

// File b.php
echo $somevar;

?>
MC Emperor
  • 22,334
  • 15
  • 80
  • 130