简体   繁体   中英

User login with php/javascript

I have a php script that I call through ajax to initialize session variables for a user login. When I retrieve the session info later on it doesn't seem to be there. What is missing from this. I must say I am assuming that the $_SESSION variable is there. I haven't done this before.

PHP code that searches for user in db and initializes session variable

while (true) {
    $md5_pw = md5($password_text);
    $r_getuser = mysql_query("SELECT * FROM users WHERE (user_name = BINARY '".mysql_real_escape_string($l_un_em)."' OR email = '".mysql_real_escape_string($l_un_em)."') AND (password = '$md5_pw' OR temp_password = '$md5_pw') AND status >= 0");
    if (mysql_num_rows($r_getuser) == 0) {
        /* echo $messages['log_un_em_pw_incorrect'];
        return; */
        $return['return_code'] = -1;
        $return['return_msg'] = $messages['log_un_em_pw_incorrect'];
        echo json_encode($return);
    }
    $row = mysql_fetch_assoc($r_getuser);
    /* if ($row['privs'] < 1) { echo $messages['log_no_privs']; break; } */
    if ($row['temp_password'] == $md5_pw) { //new password
        mysql_query("UPDATE users SET password = '".$md5_pw."', temp_password = NULL WHERE user_id = '{$row['user_id']}'");
    }
    $today = date('Y-m-d');
    if ($row['login_0'][0] == '9') { //first login
        mysql_query("UPDATE users SET login_0 = '".$today."', login_1 = '".$today."', login_cnt = 1 WHERE user_id = '{$row['user_id']}'");
    } else {
        mysql_query("UPDATE users SET login_1 = '".$today."', login_cnt = login_cnt+1 WHERE user_id = '{$row['user_id']}'");
    }
    $_SESSION['uid'] = $row['user_id'];
    $_SESSION['unm'] = stripslashes($row['user_name']);
    $_SESSION['uml'] = stripslashes($row['email']);
    $_SESSION['cL'] = $row['language'];
    /* echo '<meta http-equiv="refresh" content="0;url=livemass_CENTER34.php">'; */ //default page
    break;
}

PHP code to retrieve session variable.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

echo 'user id '.$_SESSION['uid'].'<br/>';
echo 'username '.$_SESSION['unm'].'<br/>';
echo 'uml '.$_SESSION['uml'].'<br/>';
echo 'cL '.$_SESSION['cL'].'<br/>';

?>

Javascript to show session info.

$.ajax({
      url: "get_session_info.php",
      type: "GET",
      data: {},
      cache: false,
      async: false,
      success: function (response) {
      alert ('session info response <br/>'+response);
      },
});

add session_start() to all your files which access session varibale values

<?php
session_start();
ini_set('display_errors', 1);

// remaining items

?>

http://php.net/manual/en/function.session-start.php

To build on what @Frits said. To use php sessions you must place the function session_start(); at the beginning of every page that you want to access $_SESSION on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM