2016-04-22 21 views
0

Ich versuche, meine login.php umleiten zu meiner Homepage in main.php und Echo eine eingeloggte Nachricht, wenn erfolgreich, oder echo eine erfolglose Nachricht, wenn Login fehlschlägt. Es ist der schnellen Teil des Skripts zu ignorieren und lenkt mich zu:Fehler beim Abrufen der Sitzungsvariablen

Ort: http://localhost/projects/ibill_v3/html/loginformfail.html#home

Dies ist nicht einmal existieren. Gibt es eine Möglichkeit, das zu beheben, oder mache ich es zu kompliziert? Jede Hilfe würde sehr geschätzt werden!

main.php (Homepage)

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

Ich sehe nicht, wo Sie die Sitzung beginnen, auf 'loginform' – andre3wap

+0

Ihre Abfrage * eigentlich * ein Ergebnis der Rückkehr? Ist '$ count' * wirklich * gleich' 1'? Und Sie sollten Ihre Passwörter Hashing sein. Sie im Klartext zu speichern ist eine sehr, sehr schlechte Idee. – Marcus

+0

@ andre3wap Muss ich die Sitzung in der 'loginform.php' starten. Danke Marcus, yes count ist gleich 1, wenn es auskommentiert wird und dieser Codeabschnitt hat funktioniert, bevor ich versucht habe, die Session zum Laufen zu bringen. Passwörter vor dem Abschluss hashen – asharoo85

Antwort

1

Schritt 1: Stellen Sie die Aktion auf einreichen in login.php (action = "loginform.php") loginform.php

schritt2: in loginform.php starten sie eine sitzung und ändern sie den umleitungsort in 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); 
 
?>

Schritt 3: In main.php Entfernen umfassen "loginform.php";

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

+0

Ich probierte es einfach und es hat gut funktioniert. Danke @Bitto Bennichan – asharoo85

+1

@ Asharoo85 okay. Sie sind willkommen –

Verwandte Themen