[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

Passing PHP variables from one file to another

I found this nice guide on creating a basic PHP/MySQL login:

I've already got a simple chat page that I would like to require a login to access.  I'm very new to both PHP and MySQL but I have programming/webdesign experience.  Basically all I'm after right now is a way to pass the variable "myusername" from the "checklogin.php" file to the "login_success.php" file.  Once I have that variable I'm pretty confident I can implement that in the chat replacing the user-entered name with the authenticated name.

Also, if anyone has any idea if this guide allows for session-expiration please let me know as I'm having trouble figuring out what everything means.  Thanks!
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="database_name"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
// Mysql_num_row is counting table row
// If result matched $myusername and $mypassword, table row must be 1 row
// Register $myusername, $mypassword and redirect to file "login_success.php"
else {
echo "Wrong Username or Password";

Open in new window

  • 2
  • 2
2 Solutions
You may want to look into utilizing PHP's session variables:

Adding variables to $_SESSION will allow you to carry them between scripts, as long as the user's session remains active.  Session timeouts can be adjusted in PHP.ini by changing session.gc_maxlifetime (value is in seconds).
Ray PaseurCommented:
Keys to using session successfully are these:

You  MUST put session_start() at the top of the script on every page that seeks to use the $_SESSION array.  Then you place data into the array like you would any other associative array, eg, in "checklogin.php" you would say this::

$_SESSION["myusername"] = $myusername;

And in "login_success.php" you would say this:

$myusername = $_SESSION["myusername"];

The $_SESSION array is a "superglobal" which means it is available in every namespace.  You can access it in functions and classes without any special declaration.

The PHP garbage collection routines that run at the end of the script will write the session data to a file on the server -- in other words, you do not need to do anything except put data into $_SESSION -- it is automatically saved for you.  A cookie on the client's browser lets each page retrieve the session data, but ONLY if you've issued the session_start() command.

You will have a new set of questions when you get ready to use cookies.  Please ask those here.

HTH and best regards, ~Ray
Ray PaseurCommented:
An afterthought, but a good one... If you have some IT experience and you're new to PHP, grab a copy of this book and look over the examples.  It's excellent.


A permanent part of my professional library! ~Ray

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now