dongwo8827523 2014-10-01 14:23
浏览 18
已采纳

仅显示正确的电子邮件地址会话但用户名错误

One thing I recognized is that the clause I set in the PDO Prepared is only two clauses like this:

 WHERE myemail = :myemail AND mypassword = :mypassword

So, what I want to try now is that I want to set three clause in the PDO Prepared, like this:

 WHERE myemail = :myemail, mypassword = :mypassword, AND username = :username

But I am not sure if I do that correctly. I am just learnig php (self-taught).


I ask this question is because the index page only shows the correct session of email address but wrong username.

When I log in from username of "someone" with email of "someone@mail.com", the index page show the correct username and email address with the session.

However, when I log in from username of "somebody" with email of "somebody@mail.com", the index page show the correct email (somebody@ymail.com) but with the wrong username (someone) not (somebody).

Here is my code:

// Define $myusername and $mypassword
   $myemail=$_POST['myemail']; 
   $mypassword=$_POST['mypassword']; 


// We Will prepare SQL Query
   $STM = $dbh->prepare("SELECT * FROM newuser WHERE myemail = :myemail AND mypassword = :mypassword");


// bind paramenters, Named paramenters alaways start with colon(:)
   $STM->bindParam(':myemail', $myemail);
   $STM->bindParam(':mypassword', $mypassword);

// For Executing prepared statement we will use below function
   $STM->execute();

// Count no. of records 
   $count = $STM->rowCount();

//just fetch. only gets one row. So no foreach loop needed :)
  $row  = $STM -> fetch();



// User Redirect Conditions will go here
    if ( $count == 1 )  {
       $_SESSION['login_id'] = $row['id'];
       $_SESSION['username'] = $row['username']; // added
       $_SESSION['myemail'] = $row['myemail']; // added

    if ( $_SESSION['login_id'] != '' || $_SESSION['login_id'] > 0 ) { // edited
        header("location: index.php");  
    } else { 
        header("location: login3.html");  
    }
}

 <?php
 include('UserSessionAdmin.php');
 $username = $_SESSION['username'];
 $myemail = $_SESSION['myemail'];
 ?>
 <body>
 <?php echo $username; ?>
 <?php echo $myemail; ?>
 </body>

UserSessionAdmin.php

<?php
session_start();

if ( $_SESSION['login_id'] == 0 || $_SESSION['login_id'] == '' ) {
    header('location: login.html');
    exit();
}

require_once('configPDO.php');
?>

Update:

When I added echo var_dump($row); before the line $count==1, the page shows up with this result:

array(6) { ["username"]=> string(8) "Somebody" [0]=> string(8) "Somebody" ["myemail"]=> string(17) "somebody@mail.com" [1]=> string(17) "somebody@mail.com" ["mypassword"]=> string(8) "thebest2" [2]=> string(8) "thebest2" }

  • 写回答

3条回答 默认 最新

  • dongzhun1857 2014-10-01 18:44
    关注

    Well from what you are saying this is very strange, here is how I would debug it:

     header("location: index.php?username=".$row['username]."&myemail=".$row['myemail']); 
    

    then

     <?php
     include('UserSessionAdmin.php');
     //$username = $_SESSION['username'];
     //$myemail = $_SESSION['myemail'];
     $username = $_GET['username'];
     $myemail = $_GET['myemail'];
     ?>
     <body>
     <?php echo $username; ?>
     <?php echo $myemail; ?>
     </body>
    

    This isn't a direct answer, this will help you narrow down the issue.

    If this works it is clear that the SESSION is responsible for this.


    EDIT:

    $_GET was used just for the sake of debugging, of course it is not meant for that.

    Now we know its the session, you always need to make sure it is started.

    When you set it:

    if ( $count == 1 )  {
        session_start();
        $_SESSION['login_id'] = $row['id'];
        $_SESSION['username'] = $row['username'];
        $_SESSION['myemail'] = $row['myemail'];
        ...
    }
    

    When you get it:

    include('UserSessionAdmin.php');
    session_start();//again!
    $username = $_SESSION['username'];
    $myemail = $_SESSION['myemail'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c