doujinai2183 2015-12-31 23:15
浏览 17
已采纳

用php获取id

I have a customer table. Each customer has a specific ID. In my project(ecommerce website) I want to store the ID of the user in a $_SESSION['user_id'] when he/she successfully login. How do I do that? What do I need to add? Here's my code:

<?php
// establishing the MySQLi connection
$con = mysqli_connect("localhost","root","","ecommerce");
// checking the user
if(isset($_POST['login'])){
    $email = mysqli_real_escape_string($con,$_POST['c_email']);
    $pass = mysqli_real_escape_string($con,$_POST['pass']);
    $sel_user = "select * from customer where customer_email ='$email' AND customer_pass='$pass'";
    $run_user = mysqli_query($con, $sel_user);
    $check_user = mysqli_num_rows($run_user);
    if($check_user>0){
        $_SESSION['customer_email']=$email;
        echo "<script>window.open('index.php','_self')</script>";
    } else {
        echo "<script>alert('Email or password is not correct, try again!')</script>";
    }
}
?>
  • 写回答

1条回答 默认 最新

  • douou6696 2015-12-31 23:28
    关注

    First, as @chris85 mentioned, call session_start() at the top of your script.

    Then, you're almost there. First, you need to get the result object from the results.

    $rows = array();
    
    while ($row = mysql_fetch_assoc($run_user)) {
        $rows[] = $row; // Same as array_push($rows, $row) but has better performance when pushing a single item.
    }
    

    Then, assuming we know there is only one row returned:

    $customerData = $rows[0];
    

    Cool. Now, set whatever SESSION variables you want:

    $_SESSION["user_id"] = $customerData["user_id"];
    ...
    

    Also, as has been noted in the comments, please please please do not ever store a user's password as plain text. You should hash and salt it. Here is a good starter post to read through: Best way to store password in database

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀