我正在尝试让我的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);
?>
答案 0 :(得分:1)
第1步:在login.php中将提交操作设置为loginform.php(action =&#34; loginform.php&#34;)
step2:在loginform.php中启动会话并将重定向位置更改为main.php
<?php
session_start();
$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:main.php');
exit();
}
//This checks the 'user_details' database for correct user registration details and if successful, directs to home page.
else {
header('Location: main.php');
exit();
}
//If login details are incorrect
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
?>
&#13;
第3步:在main.php中删除include&#34; loginform.php&#34;;
<?php
session_start();
if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){
echo 'working';
}
else {
echo 'not working';
}
?>
&#13;