使用数据库登录PHP
我正在尝试用MySQL登录,但它不起作用。基本上,我试图检查登录和密码张贴对我的数据库,但它不工作的某些原因。有人能给我一个提示吗 login.php使用数据库登录PHP,php,mysql,Php,Mysql,我正在尝试用MySQL登录,但它不起作用。基本上,我试图检查登录和密码张贴对我的数据库,但它不工作的某些原因。有人能给我一个提示吗 login.php include "conexao.php"; $result = mysql_query("SELECT * FROM usuario WHERE login = '".$_POST['login']."' AND senha = '".$_POST['senha']."'") or die (mysql_error()); while (
include "conexao.php";
$result = mysql_query("SELECT * FROM usuario WHERE login = '".$_POST['login']."' AND senha = '".$_POST['senha']."'") or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
session_start();
if ($_POST['login'] && $_POST['senha']) {
if ($row['login'] == $_POST['login'] && $row['senha'] == $_POST['senha']) {
$_SESSION['login'] = $row['login'];
$_SESSION['senha'] = $row['senha'];
header("Location: index.php");
} else {
unset ($_SESSION['login']);
unset ($_SESSION['senha']);
header("Location: login2.php?i=n");
}
}
}
HTML表单
<form method="post" action="login.php" class="cbp-mc-form" autocomplete="off">
<label for="login">Login</label>
<input type="text" name="login" id="login" /><br />
<label for="senha">Senha</label>
<input type="password" name="senha" id="senha" /><br />
<center><input class="cbp-mc-submit" type="submit" value="Login""/></center>
</form>
登录
森哈
从
我添加了ob_start();在第一行中,它成功了。亲爱的兄弟,请尝试以下代码(我编辑了您的代码)
我希望它能在您的情况下起作用,但如果您在生产中使用相同的代码,请注意消毒
我为您编辑的代码如下所示(如果仍然不起作用,那么您的数据库连接中可能会出现一些错误)
PHP脚本:
<?php
session_start(); // better to start the session in the begining,
//in some cases it doesn't work in the mid of the document'
include 'conexao.php';
if (isset($_POST['login']) && isset($_POST['senha'])) // check if both the form fields are set or not
{
// Values coming from the user through FORM
$login_form = $_POST['login'];
$senha_form = $_POST['senha'];
// query the database only when user submit the form with all the fields filled
$result = mysql_query("SELECT * FROM usuario WHERE login='$login_form' AND senha='$senha_form'") or die (mysql_error());
while ($row = mysql_fetch_assoc($result))
{
// values coming from Database
$login_db = $row['login'];
$senha_db = $row['senha'];
}
// compare the values from db to the values from form
if ($login_form == $login_db && $senha_form == $senha_db)
{
// Set the session only if user entered the correct username and password
// it doesn't make sense to set session even if the user entered wrong values
$_SESSION['login'] = $login_db;
$_SESSION['senha'] = $senha_db;
header("Location: index.php");
}
else
{
header("Location: login2.php?i=n");
}
}
?>
也许可以尝试使用mysql\u fetch\u数组而不是mysql\u fetch\u assocside注意:如果您将此代码用于自己的用途,那就太好了。除此之外,还有很多原因。它也不起作用。两个问题。1) 您发布的代码,是否都在同一个文件中,并且完全如图所示?2) 您的数据库连接是不是碰巧连接了mysql
还是mysqli
@GilbertoGonçalvesMachadosession_start()代码>在那里@ArifBangash-第7行
<form method="post" action="login.php" class="cbp-mc-form" autocomplete="off">
<label for="login">Login</label>
<input type="text" name="login" id="login" /><br />
<label for="senha">Senha</label>
<input type="password" name="senha" id="senha" /><br />
<center><input class="cbp-mc-submit" type="submit" value="Login""/></center>
</form>