I ventured to have my login form to be included on top of my nav bar. This works very nice but, I cannot get the session variables to hold.
Sessions in PHP have proven to be a challenge for me. I know how to use sessions, I understand the concept behind sessions, I have plenty of experience with variables and managing them but I cannot get PHP session logic under control ... They do say that every man reaches his level of incompetence; have I reached mine?
I see Sleidia code snip and it does not look like what I have (which I've done based on samples I've read on php.net).
On top of my PHP script I have
Code:
<?PHP
session_start();
global $fpMemberID, $fpMemberFirstName, $fpMemberLastName, $fpMemberSince;
/* print $GLOBALS['fpMemberID'] . "<br>";
print $GLOBALS['fpMemberFirstName'] . "<br>";
print $GLOBALS['fpMemberLastName'] . "<br>";
print $GLOBALS['fpMemberSince'] . "<br>";
*/
if(isset($_POST['submit']) && $_POST['submit'] == "Log In") {
/* $fpMemberID=$GLOBALS['fmMemberID'];
$fpMemberFirstName=$GLOBALS['fpMemberFirstName'];
$fpMemberLastName=$GLOBALS['fpMemberLastName'];
$fpMemberSince=$GLOBALS['fpMemberSince'];
*/
$_SESSION['fpMemberID'];
$_SESSION['fpMemberFirstName'];
$_SESSION['fpMemberLastName'];
$_SESSION['fpMemberSince'];
}
// Lets load the script with all the functions in it
// Kind of the same as procedure programs in dBase
include_once("include/functions.php");
// include_once("include/marketplace.php");
$Target = "main.txt" ;
if(isset($_REQUEST['Target'])):
$Target = $_REQUEST['Target'];
endif ;
if ($_POST['submit'] != "" && $_GET['Target'] == "validlogon") {
$isValid = ValidLogon($_POST['email'],$_POST['password']);
}
?>
The function ValidLogon looks like this
Code:
<?PHP
function ValidLogOn($email,$password) {
global $fpMemberID, $fpMemberFirstName, $fpMemberLastName, $fpMemberSince;
print $email . "<br />";
print $password . "<br />";
//variables for connecting to MySQL
$mysql_host = 'thehost';
$mysql_user = 'theuser';
$mysql_pass = 'password';
$mysql_db = 'thedatabase';
//set the number of records per page
$records_per_page = 15;
//connect to MySQL
mysql_connect ($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db ($mysql_db);
//find out how many records are in the table
$query = "SELECT * FROM members WHERE `MemberEmail` = '" . $email . "' AND `MemberPwd` = '" . $password . "' LIMIT 1";
$data = mysql_query ($query) or die (mysql_error());
if (mysql_num_rows($data) > 0) {
$memberInfo = mysql_fetch_assoc($data);
$fpMemberID = $memberInfo['MemberID'];
$fpMemberFirstName = $memberInfo['MemberFirstName'];
$fpMemberLastName = $memberInfo['MemberLastName'];
$fpMemberSince = $memberInfo['MemberSince'];
/* print $fpMemberID . "<br>";
print $fpMemberFirstName . "<br>";
print $fpMemberLastName . "<br>";
print $fpMemberSince . "<br>"; */
return 1;
} else {
print "Invalid User ID + Password!";
return 0; }
}
?>
If I allow the print commands to run, it does show that the record is found and variables are set. But this is only true for as long as I do not change page. The moment I move to another page, my variables go blank.
Here is where the login name and ID are validated and variables are set
Code:
<?PHP
function ValidLogOn($email,$password) {
global $fpMemberID, $fpMemberFirstName, $fpMemberLastName, $fpMemberSince;
print $email . "<br />";
print $password . "<br />";
//variables for connecting to MySQL
$mysql_host = 'thehost';
$mysql_user = 'theuser';
$mysql_pass = 'password';
$mysql_db = 'thedatabase';
//connect to MySQL
mysql_connect ($mysql_host, $mysql_user, $mysql_pass);
mysql_select_db ($mysql_db);
//find out how many records are in the table
$query = "SELECT * FROM members WHERE `MemberEmail` = '" . $email . "' AND `MemberPwd` = '" . $password . "' LIMIT 1";
$data = mysql_query ($query) or die (mysql_error());
if (mysql_num_rows($data) > 0) {
$memberInfo = mysql_fetch_assoc($data);
$fpMemberID = $memberInfo['MemberID'];
$fpMemberFirstName = $memberInfo['MemberFirstName'];
$fpMemberLastName = $memberInfo['MemberLastName'];
$fpMemberSince = $memberInfo['MemberSince'];
/* print $fpMemberID . "<br>";
print $fpMemberFirstName . "<br>";
print $fpMemberLastName . "<br>";
print $fpMemberSince . "<br>"; */
return 1;
} else {
print "Invalid User ID + Password!";
return 0; }
}
?>
I have the PHP Black Book and it dedicates an entire chapter to sessions but, it suggests one uses MySQL to store them. I might have to go that route but before I do, I sure would like to know what in the world I'm doing wrong.
I will read through the given link and learn a couple of things.
Regards,
Jose Lerebours
KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours