2016-04-22 115 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