Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: PHP Session id is not working

Qianonn Phoon:

I am new to php. I tried to use session to store the user id. However, the showSession.php print nothing in id part. Did I miss anything in the code? Thanks!

loginfb.php used to access the database


session_start();
//Load the database configuration file
require("config.inc.php");

//Convert JSON data into PHP variable
$userData = json_decode($_POST['userData']);
if(!empty($userData)){
$oauth_provider = $_POST['oauth_provider'];
//Check whether user data already exists in database
$prevQuery = "SELECT * FROM users WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$userData->id."'";
try {
$stmt = $db->prepare($prevQuery);
$result = $stmt->execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rows = $stmt->fetchAll();
if($rows){
//Update user data if already exists
$query = "UPDATE users SET first_name = '".$userData->first_name."', last_name = '".$userData->last_name."', email = '".$userData->email."', gender = '".$userData->gender."', locale = '".$userData->locale."', picture = '".$userData->picture->data->url."', link = '".$userData->link."', modified = '".date("Y-m-d H:i:s")."' WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$userData->id."'";
$update = $db->query($query);
$_SESSION["id"] = $row['id'];
}else{
//Insert user data
$query = "INSERT INTO users SET oauth_provider = '".$oauth_provider."', oauth_uid = '".$userData->id."', first_name = '".$userData->first_name."', last_name = '".$userData->last_name."', email = '".$userData->email."', gender = '".$userData->gender."', locale = '".$userData->locale."', picture = '".$userData->picture->data->url."', link = '".$userData->link."', created = '".date("Y-m-d H:i:s")."', modified = '".date("Y-m-d H:i:s")."'";
$insert = $db->query($query);
$getSession = "SELECT * FROM users WHERE oauth_provider = '".$oauth_provider."' AND oauth_uid = '".$userData->id."'";
try {
$stmt1 = $db->prepare($getSession);
$result1 = $stmt1->execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$rows1 = $stmt1->fetchAll();
if($rows1){
$_SESSION["id"] = $row['id'];
}
}

}
?>

showSession.php used to print out the session id.


session_start();
echo session_id();
$id=$_SESSION['id'];

echo "Session id is " . $_SESSION["id"] . ".
";
echo "Session id is " . $id . ".
";
print_r($_SESSION);
?>

login.html is the user interface to let user login with facebook account.









Login Form
















Remember me




Forgot password?












Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: PHP Session id is not working

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×