Hi guys,
I have an issue with SESSIONS in PHP and tried to find out why for 2 days and no success. This is a login page below divided into to login sections,
1- Facebook Login: Logins successfully, captures user data but wouldn't set sessions.
2- Custom Login: loges in fine and but same issue with sessions. It should create a session and redirect users to user folder if session exists,
<?php
if (isset($_SESSION['login_token']) && isset($_SESSION['account_no'])) {header('Location:../user/index.php');}
//if (!isset($_SESSION['login_token']) || !isset($_SESSION['account_no'])) {header('Location:../login/index.php');}
?>
<?php include_once('../library/header.php'); ?>
<?php
require '../library/facebook.php';
/// login
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '484846211552634',
'secret' => '1ce89585da85f6c8ff877ab175a144fb',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_birthday, user_hometown, email,user_location, publish_stream'));
}
if ($user) {
$account_no=mt_rand();
//// check if correct
$select=mysql_query("SELECT * FROM users WHERE email='{$user_profile[email]}' AND suspended='1'");
if (mysql_num_rows($select)>=1) { header('Location:../index.php?msg=suspended');}
else {
$select_account_no=mysql_query("SELECT * FROM users WHERE account_no='$account_no'");
if (mysql_num_rows($select_account_no)==0) {
$select_email=mysql_query("SELECT * FROM users WHERE email='{$user_profile[email]}'");
if (mysql_num_rows($select_email)==0) {
$account_no=mt_rand();
$login_token=sha1($account_no);
$reg_date=date('Y-m-d');
$insert=mysql_query("INSERT INTO users (account_no, first_name, last_name, email, reg_date, city, reg_via, login_token) VALUES ('$account_no','{$user_profile[first_name]}','{$user_profile[last_name]}','{$user_profile[email]}','$reg_date', '{$user_profile[user_hometown]}','1','$login_token')");
$_SESSION['login_token']=$login_token;
$_SESSION['account_no']=$account_no;
if (mysql_num_rows($select_email)>=1) {
$select_my_email=mysql_query("SELECT * FROM users WHERE email='{$user_profile[email]} AND suspended='0''");
while ($row=mysql_fetch_array($select_my_email)) {
$myaccount_no=$row['account_no'];
$mylogin_token=$row['login_token'];
$_SESSION['login_token']=$mylogin_token;
$_SESSION['account_no']=$myaccount_no;
echo"$myaccount_no";
}}}}} }
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<?php login(); ?>
<div class="box">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" class="login" method="post" id="login">
<h1 class='-login-title'>Already have an account</h1>
<section class="state-normal section-login">
<label for="email">Email</label>
<input type="email" name="email" class="validate[required,custom[email]] text-input"/>
</section>
<section class="state-normal">
<label for="password">Password</label>
<input type="password" name="password" class="validate[required] text-input"/>
</section>
<p><a href="/recovery" title="Recover your password">Can't remember your password?</a></p>
<section class="state-normal">
<input type="checkbox" name="checkbox" id="checkbox1" />
<label for="checkbox1">Remember me</label>
</section>
<button type="button" onclick="window.location = '../register/'" class="color-blue">Create a new account</button>
<button type="submit" class="color-grey" name="login">Login</button>
</form>
<div class="fb"><h1 class='fb-login-title'>Or login with Facebook</h1>
<?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</a><?php else: ?><a href="<?php echo $loginUrl; ?>"><img src="../img/facebook-connect.png" alt="Connect using Facebook" width="269" height="56" class="fb-connect-button"/></a><?php endif ?>
<p class="text-central">Skip registration using your Facebook Account</p>
</div>
<?php include_once('../library/footer.php'); ?>