【发布时间】:2016-04-23 00:44:52
【问题描述】:
我正在尝试让我的login.php 重定向到我在main.php 中的主页,并在成功时回显登录消息,或者在登录失败时回显不成功消息。它忽略了脚本的快速部分并指导我:
地点:http://localhost/projects/ibill_v3/html/loginformfail.html#home
这甚至不存在。有没有办法解决这个问题还是我让它太复杂了?任何帮助将不胜感激!
main.php(主页)
<?php
session_start();
include "loginform.php";
if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){
echo 'working';
}
else {
echo 'not working';
}
?>
loginform.php
<?php
$con=mysqli_connect('localhost','root','cornwall','ibill');
// This code creates a connection to the MySQL database in PHPMyAdmin named 'ibill':
$username="";
$password="";
if (isset ($_POST['username'])){
$username = mysqli_real_escape_string($con, $_POST['username']);
}
if (isset ($_POST['password'])){
$password = mysqli_real_escape_string($con, $_POST['password']);
}
//These are the different PHP variables that store my posted data.
$login="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result=mysqli_query($con, $login);
$count=mysqli_num_rows($result);
//This is the query that will be sent to the MySQL server.
if($count==1)
{
$_SESSION["user_session"]=$username;
header('Location: http://localhost/projects/ibill_v3/html/main.php#home');
exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
header('Location: http://localhost/projects/ibill_v3/html/loginformfail.html');
exit();
}
//If login details are incorrect
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>
【问题讨论】:
-
我看不到您在
loginform上开始会话的位置 -
您的查询实际上是否返回了结果?
$count真的等于1吗?你应该散列你的密码。以纯文本形式存储它们是一个非常非常糟糕的主意。 -
@andre3wap 我需要在“loginform.php”上启动会话吗?谢谢马库斯,是的,注释掉时计数等于 1,并且在我试图让会话正常工作之前,该部分代码正在工作。将在完成前对密码进行哈希处理
-
@asharoo85 "我是否需要在 'loginform.php' 上启动会话。" 不需要。这不是必需的,会发出通知。您只需要启动一次会话,因为它将在
include'd 文件的范围内。 -
请使用 PHP 的built-in functions 来处理密码安全问题。如果您使用低于 5.5 的 PHP 版本,您可以使用
password_hash()compatibility pack。在散列之前,请确保您 don't escape passwords 或对它们使用任何其他清理机制。这样做会更改密码并导致不必要的额外编码。
标签: php html session jquery-mobile login