We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Passing PHP variables from one file to another

CauseLs
CauseLs asked
on
Medium Priority
955 Views
Last Modified: 2013-12-12
I found this nice guide on creating a basic PHP/MySQL login:
http://www.phpeasystep.com/workshopview.php?id=6

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!
====checklogin.php=====
 
<?php
ob_start();
$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
$myusername=$_POST['myusername'];
$mypassword=$_POST['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'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
 
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:../login_success.php");
}
else {
echo "Wrong Username or Password";
}
 
ob_end_flush();
?>
 
 
 
 
 
====login_success.php====
 
<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
 
<html>
<body>
</body>
</html>

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
You may want to look into utilizing PHP's session variables:
http://us2.php.net/session

Examples:
http://us2.php.net/manual/en/session.examples.basic.php

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
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).
Most Valuable Expert 2011
Author of the Year 2014
Commented:
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
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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.

http://www.sitepoint.com/books/phpmysql1/

A permanent part of my professional library! ~Ray
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.