2016-04-22 10 views
0

私のlogin.phpmain.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、コメント数が1で、セッションの動作を試みる前にそのコードセクションが動作していた場合、イエス数は1になります。完了前にパスワードをハッシングするでしょうか? – asharoo85

答えて

1

ステップ1:login.phpにloginform.phpし提出する上でアクションを設定(アクション= "loginform.phpを")

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); 
 
?>

ステップ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 okay.you歓迎です –