2016-04-01 23 views
0

Oturum açma başarılı olduğunda, kullanıcı sitemin ana sayfasına yönlendirilir. Kodum yanlış giriş bilgilerini (login_parse.php) girdiklerinde kullanıcıyı boş bir sayfaya yönlendirir ve burada 'Giriş başarısız' hata mesajı görüntülenir. Giriş sayfasında (index.php) hata mesajının görünmesini istiyorum.Oturum açma hatası iletisi, giriş sayfasında php ile nasıl görüntülenir

Giriş sayfasını:

<?php session_start(); ?> 
<!DOCTYPE html> 
<html> 

<link rel="stylesheet" type="text/css" href="style.css"> 

<head> 
</head> 

<body> 


<form id='registration_form' action='registration.php' method='post'> 
    <input id ='new_username' type='text' name='new_username'  placeholder='Create a username' oninvalid="this.setCustomValidity('Please create a username')"required></input> 
    <input id ='new_password' type='password' name='new_password'  placeholder='Create a password' oninvalid="this.setCustomValidity('Please create  a password')" required></input> 
    <input id ='new_email' type='email' name='new_email' placeholder='Enter a valid student.lboro.ac.uk email  address'oninvalid="this.setCustomValidity('Please enter a valid  student.lboro.ac.uk email address')" required></input> 
    <input id ='register_button' type='submit' value='Sign Up'/> 
</form> 

</body> 
</html> 

login_parse.php: Giriş filtresi ve PDO için http://www.phptherightway.com/#pdo_extension de

<?php 

session_start(); 

include "dbconnect.php"; 

if (isset($_POST['username'])) { 

    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $sql = "SELECT * FROM Users WHERE username= '".$username."' AND password='".$password."' LIMIT 1"; 
    $res = mysqli_query($db, $sql) or die(mysqli_error); 

     if (mysqli_num_rows($res) == 1) { 

      $row = mysqli_fetch_array($res); 
      $_SESSION['uid'] = $row['id']; 
      $_SESSION['username'] = $row['username']; 
      header("Location: shownewposts.php"); 
      exit(); 


      } 
      else { 

      echo "Login unsuccessful"; 
      exit(); 
      } 

} 

?> 
+0

[. Senaryonuz SQL Injection Saldırıları riski taşıyıp taşımadığının] (http://stackoverflow.com/questions/60174/nasıl yapılır-nasıl-sql-injection-in-php) [MySQLi] için hazırlanmış (http://en.wikipedia.org/wiki/Prepared_statement) ifadeleri hakkında bilgi edinin (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). –

+0

Parola güvenliğini işlemek için lütfen PHP'nin [built-in işlevleri] (http://jayblanchard.net/proper_password_hashing_with_PHP.html) kullanın. PHP sürümü 5.5'ten küçükse, 'password_hash()' [uyumluluk paketi] 'ni (https://github.com/ircmaxell/password_compat) kullanabilirsiniz. –

+0

Aynı sayfada kalmak istiyorsanız, sayfayı yeniden yüklemek istemediğiniz sürece JavaScript ve AJAX kullanın. –

cevap

0

bak.

Başka bir deyimde oturum değişkeni oluşturabilirsiniz. şey gibi:

<?php 

session_start(); 

include "dbconnect.php"; 

if (isset($_POST['username'])) { 
    unset($_SESSION['errorMessage']); 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $sql = "SELECT * FROM Users WHERE username= '".$username."' AND password='".$password."' LIMIT 1"; 
    $res = mysqli_query($db, $sql) or die(mysqli_error); 

    if (mysqli_num_rows($res) == 1) { 

     $row = mysqli_fetch_array($res); 
     $_SESSION['uid'] = $row['id']; 
     $_SESSION['username'] = $row['username']; 
     header("Location: shownewposts.php"); 
     exit(); 


    } 
    else { 
     $_SESSION['errorMessage'] = 1; 
     header("Location: index.php"); 
     exit(); 
    } 

} 

?> 

Ve seans variabile varsa index.php kontrol ve baskı mesajında ​​

<?php session_start(); ?> 
<!DOCTYPE html> 
<html> 

<link rel="stylesheet" type="text/css" href="style.css"> 

<head> 
</head> 

<body> 

<?php 
    if (isset($_SESSION['errorMessage']){ 
    echo "<span style='color:red;'>Check your input</span>"; 
    } 
?> 
<form id='registration_form' action='registration.php' method='post'> 
    <input id ='new_username' type='text' name='new_username'  placeholder='Create a username' oninvalid="this.setCustomValidity('Please create a username')"required></input> 
    <input id ='new_password' type='password' name='new_password'  placeholder='Create a password' oninvalid="this.setCustomValidity('Please create  a password')" required></input> 
    <input id ='new_email' type='email' name='new_email' placeholder='Enter a valid student.lboro.ac.uk email  address'oninvalid="this.setCustomValidity('Please enter a valid  student.lboro.ac.uk email address')" required></input> 
    <input id ='register_button' type='submit' value='Sign Up'/> 
</form> 

</body> 
</html> 
İlgili konular