2014-07-21 18 views
0

因此,这里是我的网站登录页面的代码,我希望它在用户登录并将其存储在会话中时从数据库中拉出用户ID数据。我将在另一个页面中使用来从另一个表中提取值。从数据库中通过电子邮件将userId拉出并在会话中存储用户ID

<?php 
session_start(); 
//Connect to DB 
include_once 'db_connect.php'; 

// escape variables for security 
$Email = mysqli_real_escape_string($mysqli, $_POST['email']); 
$Password = mysqli_real_escape_string($mysqli, $_POST['password']); 
//password hashing 
$Password = hash("sha256", $Password); 

$sqli="SELECT * FROM users WHERE Email='$Email' and Password='$Password'"; 
$result=mysqli_query($mysqli, $sqli); 

//Mysql_num_row is counting table row 
$count=mysqli_num_rows($result); 



// If result matched $Email and $Password, table row must be 1 row 
if($count==1){ 

//redirect to file "menu.html" 

header("location:menu.html"); 
} 
//If all else fails they shall not pass! 
else { 
echo "Wrong Email or Password. YOU SHALL NOT PASS!"; 
} 
?> 
+0

代码将从安全性得到改善,这是更多的获得功能工作版本,然后擦亮它 –

+0

我没有看到一个问题? – Steve

+0

问题是我如何获取用户标识,然后将其存储在会话数据中? –

回答

1

要在数据库中使用mysqli_fetch_arraymysqli_fetch_assoc

插入此下方的$导致

while($row = mysqli_fetch_array($result)): //fetching the row from database 

    $id = $row['id']; //the id column in your table 
    $_SESSION['id'] = $id; //the id is now saved in your session for future use 

endwhile; 

得到表中的ID在另一个页面不忘记session_start();访问$ _SESSION ['id'],以便您可以从数据库的表中访问与该ID相关的不同信息。

0
if ($count == 1) { 
    //redirect to file "menu.html" 
    While ($row = mysql_fetch_array($result)) { 
     $userName = $row['username']; // Where username is the user ID value in your db 
     $_SESSION['username'] = $userName; 
     header("location:menu.html"); 
    } 
} 
0

好的,所以你需要做的是从数据库中获取用户ID。假设该users表有一个称为id柱:

$result=mysqli_query($mysqli, $sqli); 

//Mysql_num_row is counting table row 
$count=mysqli_num_rows($result); 

// If result matched $Email and $Password, table row must be 1 row 
if($count==1){ 

    $row = mysql_fetch_array($result); 
    $_SESSION['userID']=$row['id']; 

    header("location:menu.html"); 
    die(); 
} 

注意mysql_fetch_array通常被称为while循环中,获得多行。但是,在这里你只有一行,所以这是不需要的

相关问题