2013-01-20 24 views
-1

我試圖瞭解如何建立一個用戶註冊與PHP和MySQL。PHP註冊與會話和用戶訪問

我已經構建了一個表格,用戶可以填寫並將信息存儲在我的表格中。

error_reporting(E_ALL); 
include_once ('connection.php'); 

// Required field names 
$required = array('firstname', 'lastname', 'email', 'password', 'accounttype'); 

// Loop over field names, make sure each one exists and is not empty 
$error = false; 
foreach($required as $field) { 
    if (empty($_POST[$field])) { 
    $error = true; 
    } 
} 

if ($error) { 
    echo "All fields are required."; 
} else { 
    $firstname = $_POST['firstname']; 
    $lastname = $_POST['lastname']; 
    $email = $_POST['email']; 
    $password = md5($_POST['password']); 
    $accounttype = $_POST['accounttype']; 

    $query = "INSERT INTO users(firstname,lastname,email,password,accounttype) VALUES (:firstname,:lastname,:email,:password,:accounttype)"; 
     $stmt = $dbh->prepare($query); 
     $stmt->bindParam(':firstname', $firstname); 
     $stmt->bindParam(':lastname', $lastname); 
     $stmt->bindParam(':email', $email); 
     $stmt->bindParam(':password', $password); 
     $stmt->bindParam(':accounttype', $accounttype); 
     $stmt->execute(); 

    if(!$query){ 
     echo 'Whoops, something went wrong!'; 
    } else { 
     echo $accounttype; 
     if($accounttype == '1'){ 
      header ('Location: /england/dashboard.php'); 
      exit; 
     }; 
     if($accounttype == '2'){    
      header ('Location: /ireland/dashboard.php'); 
      exit; 
     }; 
    }; 
}; 

當用戶完成表單時,他們根據其帳戶類型被重新分配到不同的頁面。

在這些頁面上,我需要以某種方式檢查用戶是否屬於accounttype'X'。所以,如果他們在

header ('Location: /ireland/dashboard.php'); 

登陸他們的賬戶類型值將等於2,因此只有與人的賬戶類型2可以訪問上面提到的。

我讀過關於會話變量,但我在哪裏設置這些?

+0

請參閱http://php.net/session.examples.basic – mario

回答

2
session_start(); // this at top of page 
if($accounttype == '1'){ 
     $_SESSION['accountType'] = 1; // or $accounttype 
     header ('Location: /england/dashboard.php'); 
     exit(); 
    }; 
    if($accounttype == '2'){ 
     $_SESSION['accountType'] = 2; // or $accounttype   
     header ('Location: /ireland/dashboard.php'); 
     exit(); 
    }; 

在英格蘭/ dashboard.php

session_start(); 
if($_SESSION['accountType'] !== 1) header('location: login.php'); 

在愛爾蘭/ dashboard.php

session_start();  
if($_SESSION['accountType'] !== 2) header('location: login.php'); 
0

開始,你的建築形式的會議,

session_start(); 
$_SESSION['account_type'] = 2; 

,並在dashboard.php只是讓你的會話變量來檢查帳戶類型。

if(($_SESSION['account_type'] == 2)) { 
header('`dashboard.php'); 
} else { 
// someother page or restrict access 
} 
0

只需用session_start();

分配會話開始你的PHP腳本$_SESSION['whatever'] = "something";

瓦爾您必須session_start();開始你希望雖然使用會話變量任何網頁上的腳本。

要銷燬會話和所有相關的增值經銷商乾脆用session_destroy();

0

一種方式做到這一點:

  • 使用「頭/配置」文件,您require_once()每一頁
  • 在上這個文件存儲信息在會話變量中是這樣的:

    $_SESSION['myCustomValue'] = $accountType; 
    
  • 在此基礎上什麼是存儲在那裏你可以重定向:

    if ($_SESSION['myCustomValue'] = 2): 
        header ('Location: /ireland/dashboard.php'); // oh yea! 
    endif; 
    
0

首先至少SHA1哈希密碼。將結果存儲在數據庫中,而不是實際的密碼。爲了測試登錄,你SHA1哈希他們給你並且比較哈希值。在散列之前,你還應該使用密碼,但只是哈希會是一個好的開始。

還給你的用戶記錄一個可以用作主鍵的id。

你基本上在腳本中做了start_session()第一件事。這將開始一個新的或附加到他們有。

然後,他們登錄/註冊後,你知道他們的用戶ID是存儲在$ _SESSION ['userid'] = $ userid的會話中;

要測試登錄:isset($ _ SESSION ['userid'])將返回true。

編輯

一旦你改變你的表有ID作爲自動遞增,主鍵,您插入件之上並不需要改變,但可以通過調用$ dbh-> lastInsertId獲取ID( )

0

您需要決定要在session data中存儲的內容。當一個人完成的形式,通過驗證並保存在數據庫中,你可能會想這樣做:

if(!$query) { 
    echo 'Whoops, something went wrong!'; 
} else { 
    session_start(); 
    $_SESSION['account_type'] = $accounttype; 

    // Carry on functionality... 
} 

而且在腳本的開頭,你可以防止現有用戶訪問登記表:

session_start(); 

if(isset($_SESSION['account_type'])) { 
    header('Location: /ireland/dashboard.php'); 
}