2012-01-20 47 views
0

我使用我的網站上對這些文件我在這裏包括了mysqli的登錄系統:Msqli PHP fetch_object

(login.php中):

<?php 
session_start(); 
require_once 'classes/Membership.php'; 
$membership = new Membership(); 

// If the user clicks the "Log Out" link on the index page. 
if(isset($_GET['status']) && $_GET['status'] == 'loggedout') { 
    $membership->log_User_Out(); 
} 

// Did the user enter a password/username and click submit? 
if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) { 
    $response = $membership->validate_User($_POST['username'], $_POST['pwd']); 
} 


?> 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

etc. 

這是主要的HTML文件和以下是php文件:

(membership.php);

<?php 

require 'Mysql.php'; 

class Membership { 

    function validate_user($un, $pwd) { 
     $mysql = New Mysql(); 
     $ensure_credentials = $mysql->verify_Username_and_Pass($un, $pwd); 

     if($ensure_credentials) { 
      $_SESSION['status'] = 'authorized'; 
proposed session variable: $_SESSION['name'] = 'RESULT'; 
      header("location: index.php"); 
     } else return "Please enter a correct username and password"; 

    } 

    function log_User_Out() { 
     if(isset($_SESSION['status'])) { 
      unset($_SESSION['status']); 

      if(isset($_COOKIE[session_name()])) 
       setcookie(session_name(), '', time() - 1000); 
       session_destroy(); 
     } 
    } 

    function confirm_Member() { 
     session_start(); 
     if($_SESSION['status'] !='authorized') header("location: login.php"); 
    } 

} 
?> 

(mysql.php):

<?php 

require_once 'includes/constants.php'; 

class Mysql { 
    private $conn; 

    function __construct() { 
     $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
         die('There was a problem connecting to the database.'); 
    } 

    function verify_Username_and_Pass($un, $pwd) { 

     $query = "SELECT * 
       FROM players_login 
       WHERE username = ? AND password = ? 
       LIMIT 1"; 

     if($stmt = $this->conn->prepare($query)) { 
      $stmt->bind_param('ss', $un, $pwd); 
    My Attempt: $row = $query->fetch_object(); 
      $stmt->execute(); 


      if($stmt->fetch()) { 
       $stmt->close(); 
       return true; 
      } 
     } 

    } 
} 

右鍵我的查詢是我想的服務器呼應用戶的名稱在index.php說例如Hello: Jane Thompson一個必須使用的能力名稱當他們發佈任何東西到數據庫,即:Posted By: Jane Thompson

+0

請注意,問候和簽名不應該包括在問題: http://meta.stackexchange.com/questions/2950 ;-) – DaveRandom

+0

你真的只是說_Feel隨時改變代碼只要它工作_?我想你可能會誤解我們在這裏所做的...... – Repox

+0

對不起,我只是需要知道它爲什麼給我一個錯誤。沒有違法意圖。我只想要一些建議。 –

回答

1

可以爲您節省在$ _SESSION的用戶名和密碼,如:

if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) { 
    $response = $membership->validate_User($_POST['username'], $_POST['pwd']); 
    $_SESSION['user'] = $_POST['username']; 
} 

所以在index.php文件,你可以打印

<div>Name: <?php echo $_SESSION['user']?></div> 
+0

其實真的很感謝你的代碼讓我變瘦了一個解決方法。萬分感謝!! –

+0

管理數據庫有一個這樣的表:用戶名|傳遞| dispName。 $ stmt-> fetch()不返回行/ s? – JackTurky