输入无效的用户名或密码时,pdo echo错误消息 [英] pdo echo error message when invalid username or password is entered

查看:94
本文介绍了输入无效的用户名或密码时,pdo echo错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是pdo(php)的新手,我创建了一个基本的登录系统,我知道它并不安全,但是无论如何我还是在做实验,我知道在旧的php中,if语句中可能会出现回显和错误消息但这似乎不起作用了,这是我的脚本,是我做错了什么,或者您再也无法在pdo中做到这一点.

hello i am relatively new to pdo(php) and i created a basic login system i know it is not secure yet but i am just experimenting anyway i know in the old php you could echo out and error message within an if statement but it does not seem to work anymore here is my script is it that im doing something wrong or that you just cant do that in pdo anymore.

if ($row == null){

            header( "location: login.html");
            echo "login failed";

        } else{

            header("location: homepage.php");
        }

我意识到这可能没有足够的代码,所以这里是脚本的其余部分

i realize that this may not have enough code available so here is the rest of the script

session_start();
    //connection String
    $connection = new PDO("sqlsrv:server=server;Database=database", "username", "password"); 

    //Seelcting function
    $smt = $connection->prepare("select user_id, username from account where username = :username and password =:password");

    //setting values to textboxes
    $username = $_POST["txt_username"];
    $password = $_POST["txt_password"];

    //binding values
    $smt->bindValue(':username', $username);
    $smt->bindValue(':password', $password);

    //execution
    $smt->execute();

    //fetching data
    $row = $smt->fetch( PDO::FETCH_ASSOC ) ;  
    echo "$row[user_id]\n\n";
    echo "$row[username]\n\n";
    $_SESSION{"user_id"} = $row["user_id"];

推荐答案

发送

header( "location: login.html");

浏览器将重定向到该新文件(login.html),并忽略(几乎)任何其他输出.

the browser will redirect to that new file (login.html) and ignore (almost) any further output.

稍后要在login.html上显示消息,您必须使用某种机制将消息传输到该页面,例如,使用会话变量.

Do display the message on login.html later on, you have to use some mechanism to transfer the message to that page, e.g., by using a session variable.

编辑

header命令在实际内容之前向浏览器发送某种数据.如果您使用标题使浏览器重定向,则用户将永远无法看到内容.

The header command send some kind of data to the browser before the actual content. If you make the browser redirect using a header, the user never gets to see the content.

因此,您需要某种方式将内容重定向到下一页.

So you need some way of bringing the content to the next page, you are redirecting to.

一种可能性是使用会话变量.

One possibility would be to use a session variable.

if ($row == null){
  $_SESSION['errormsg'] = "login failed";
  header( "location: login.php");
} else{
  header("location: homepage.php");
}

login.php中,如果出现此消息,您可以对此消息做出反应:

In the login.php then you could react to this message if it is present:

if( isset( $_SESSION['errormsg'] ) ) {
  // do the output
  echo $_SESSION['errormsg'];
  // delete the message from the session, so that we show it only once
  unset( $_SESSION['errormsg'] );
}

这篇关于输入无效的用户名或密码时,pdo echo错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆