2016-04-22 119 views
0

我想让我的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); 
?> 
+3

我不知道你在哪里开始的会议上'loginform' – andre3wap

+0

是您查询* *实际返回结果? '$ count' *真的*等于'1'吗?你应该哈希你的密码。以纯文本存储它们是一个非常糟糕的主意。 – Marcus

+0

@ andre3wap我是否需要在'loginform.php'上启动会话。感谢Marcus,在注释掉之前,count是等于1,并且在我试图让会话正常工作之前,这段代码正在工作。将完成之前哈希密码 – asharoo85

回答

1

第1步:设置在提交给login.php中的loginform.php行动(ACTION = “loginform.php”)

第2步:在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); 
 
?>

步骤3:在main.php删除包括 “loginform.php”;

<?php 
 
    session_start(); 
 
if (isset($_SESSION['user_session']) and $_SESSION['user_session']!=""){ 
 
    echo 'working'; 
 
} 
 
else { 
 
    echo 'not working'; 
 
} 
 
?>

+0

刚刚尝试过它,它工作得很好。谢谢@Bitto Bennichan – asharoo85

+1

@ asharoo85好的,欢迎您 – 2016-04-22 21:25:06