Passing PHP variables from one file to another

Posted on 2009-02-17
Last Modified: 2013-12-12
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

Question by:CauseLs
    LVL 15

    Accepted Solution

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

    LVL 15

    Expert Comment

    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).
    LVL 107

    Assisted Solution

    by:Ray Paseur
    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
    LVL 107

    Expert Comment

    by:Ray Paseur
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now