dongyou8368 2013-04-13 05:24
浏览 42
已采纳

通过函数运行“isset”时,会话不会启动

I'm creating a valid credential function for a log in page.
The function works if I use it outside the function using the following code:

if(isset($_POST['email'])){
    $email = $_POST['email'];
    $password = $_POST['password'];

    $q = "SELECT * FROM user WHERE email = :email";
    $query = $db->prepare($q);
    $query->execute(array(":email" => $email));
    $results = $query->fetchAll();

    if($results !=FALSE && $query->rowCount() > 0) {
    if($results[0]['password'] == $password){
    $_SESSION['email'] = $email;
}

But if I call the function from the external function, nothing happens as if there was something wrong with the log in credentials.

the function:

 function valid_credientials($email,$password){
    global $db;

    $email = $_POST['email'];
    $password = $_POST['password'];
    $q = "SELECT * FROM user WHERE email = :email";
    $query = $db->prepare($q);
    $query->execute(array(":email" => $email));
    $results = $query->fetchAll();
    if($results !=FALSE && $query->rowCount() > 0) {
    if($results[0]['password'] == $password){
    $_SESSION['email'] = $email;

}}

}

and the isset post:

 if (isset($_POST['email'], $_POST['password'])){
        if (valid_credientials($_POST['email'], $_POST['password']) == false ){
        $errors[] = 'No matching email found.';
    }
        if (empty($errors)){
            $_SESSION['email'] = htmlentities($_POST['email']);

            header("Location: profile.php");
                die();

        }
    }

I was thinking about trouble with the connection, but if I use this function it returns the emails.

function valid_credientials($email,$password){
    $test = $db->query("SELECT `email` FROM `user`");
    while($row = $test->fetch(PDO::FETCH_ASSOC)){ 
        echo $row['email'], '<br>';
    } 
}
  • 写回答

2条回答 默认 最新

  • dongshubang7816 2013-04-13 05:44
    关注

    Look at your code,

    function valid_credientials($email,$password){
        global $db;
    
        $email = $_POST['email'];
        $password = $_POST['password'];
        $q = "SELECT * FROM user WHERE email = :email";
        $query = $db->prepare($q);
        $query->execute(array(":email" => $email));
        $results = $query->fetchAll();
        if($results !=FALSE && $query->rowCount() > 0) {
        if($results[0]['password'] == $password){
           $_SESSION['email'] = $email;
    
        }
    }
    

    You are getting values from $_POST, you don't need these two line at all as you have email and password in your function parameters.

    $email = $_POST['email'];
    $password = $_POST['password'];
    

    And also you check for return value of the function but there is nothing returned by your function. go through the code below, it returns true or false depending on the condition.

    function valid_credientials($email,$password){
        global $db;
    
        $email = $_POST['email'];
        $password = $_POST['password'];
        $q = "SELECT * FROM user WHERE email = :email";
        $query = $db->prepare($q);
        $query->execute(array(":email" => $email));
        $results = $query->fetchAll();
        if($results !=FALSE && $query->rowCount() > 0) {
        if($results[0]['password'] == $password){
           $_SESSION['email'] = $email;
           return true;
        }
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示