drctyr2869 2013-04-23 10:23
浏览 45
已采纳

使用$ _SESSION跟踪CMS中的多个用户

Hello I am trying to make multiple users in a CMS I made. I have all their data in a table and was using mysql_num_rows check if the records matched and then use session_register() to set a session. I have changed this to PDO commands.

I want to be able to track the user when they are using the CMS so that every record changed can have their usrID attached to it. So that at a later date I can see who made updates and eventually use this to show information about the author etc.

So for example when they use forms to update or add a new record a hidden input with have their session id echo'd into it which will be taken from their user record as they log in.

Is the best way to do this? Have a written the syntax in this login code correctly?

$con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$sql="SELECT * FROM $tbl_name WHERE the_username='$the_username' and the_password='$the_password'";

$result = $con->prepare($sql); 
$result->execute(); 
$number_of_rows = $result->fetchColumn();

if($number_of_rows==1){
    $info = $result->fetch(PDO::FETCH_ASSOC);
    $_SESSION['username'] = $info['the_username'];
    $_SESSION['id'] = $info['id'];
    header('Location: admin.php');
}else{
echo "Wrong username or password, please refresh and <a href=\"login.php\">try again.</a>";
}

Would it perhaps be better to put?

if($number_of_rows==1 && $info = $result->fetch(PDO::FETCH_ASSOC)){MAKE SESSION}
  • 写回答

2条回答 默认 最新

  • douchao1879 2013-04-23 11:15
    关注

    Your usage of PDO functions is quite inconsistent, and it leads to some errors.

    First of all, you cannot fetch the same data twice. And, as a matter of fact, you don't need such a double fetch at all.

    Also, for some reason you are not using prepared statements which are the only reason for using PDO. So, the proper code would be

    $sql="SELECT * FROM $tbl_name WHERE the_username=? and the_password=?";
    $result = $con->prepare($sql); 
    $result->execute(array($the_username,$the_password)); 
    # $number_of_rows = $result->fetchColumn(); <- don't need that
    $info = $result->fetch();
    if($info){
        $_SESSION['username'] = $info['the_username'];
        $_SESSION['id'] = $info['id'];
        header('Location: admin.php');
    }else{
        echo "Wrong username or password, please refresh and <a href=\"login.php\">try again.</a>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据