使用PHP和mysqli登录 [英] Login using PHP and mysqli

查看:53
本文介绍了使用PHP和mysqli登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用php和mysqli登录,这是目前的情况:

so I was trying to do a login with php and mysqli and this is how its going at the moment:

$DBServer = 'X'; // ip o lo que sea
$DBUser   = 'X';
$DBPass   = 'X';
$DBName   = 'X';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

if ($_POST['login']) {
    $usuario = (isset($_POST['usuario'])) ? $_POST['usuario'] : '';
    $pass = (isset($_POST['pass'])) ? $_POST['pass'] : '';
if ($usuario == "" or $pass == "") {
    echo 'Rellena todos los campos';
} else {
$query = "SELECT * FROM users WHERE user = ? AND password = ? LIMIT 1";
    $stmt=$conn->prepare($query);

    $stmt->bind_param("ss",$usuario,$pass);

    $stmt->execute(); 

    $stmt->bind_result($nombre_db,$pass_db);

    while ($stmt->fetch()) {
    if ($nombre_db != $usuario) {
            echo'No existe el usuario';
        } else { 
        if ($pass_db !== $pass) { 
            echo'Contraseña incorrecta';
        } else { //pass correcto
        session_start();
            $_SESSION["nombre"]=$usuario;
            echo'<META HTTP-EQUIV="REFRESH" CONTENT="0";URL="home.php">';
    }
    }
}

但是,当我尝试登录时,没有任何反应.知道我在做什么错吗? 表格用户有两列:用户和密码.此外,此代码应放在哪里?

However, when I try log in nothing happens. Any idea what am I doing wrong? The table users has two columns: user and password.Futhermore, where should this code go?

$stmt->close();

$mysqli->close();

谢谢! 运行此代码时显示的错误:

Thanks! The errors displayed when running this code:

error_reporting(E_ALL);
ini_set('display_errors', '1');

以下

通知:未定义的索引:在第39行的/home/u949068087/public_html/web/admin/login.php中登录

Notice: Undefined index: login in /home/u949068087/public_html/web/admin/login.php on line 39

第39行如下: if($ _ POST ['login']){ 实际形式如下:

Line 39 is the following: if($_POST['login']){ The actual form is the following:

<form method="post">
  <p>Usuario:
 <input type="text" name="usuario"  />
  </p>
  <p>
    Contraseña:
    <input type="password" name="pass"  />
    </p>
  <p>
    <input type="submit" value="login" name="login"  />
  </p>
</form>

我通过的内容如下:

Array([usuario] => exampleuser [pass] => examplepassword [login] => login) 就我而言,错误在于代码的这一部分,而不是execu

Array ( [usuario] => exampleuser [pass] => examplepassword [login] => login ) EDIT 2: As far as I am concerned, the error is in this part of the code which is not execu

while($stmt->fetch()){}

有什么想法吗? 再次感谢!

Any ideas? Thanks again!

推荐答案

代码末尾缺少一个括号,但是我想您在此处复制代码时错过了它.确定是否可以.

There is one bracket missing in the end of your code, but I suppose you missed it when you copied your code here. Make sure if it is OK.

第二件事.您显示PHP错误吗?您可以将此代码放在PHP文件的开头.

Second thing. Do you show PHP errors? You can put this code in the beggining of your PHP file.

error_reporting(E_ALL);
ini_set('display_errors', '1');

这篇关于使用PHP和mysqli登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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