2016-05-19 7 views
-4

私は1ページ(index.php)に2つのフォーム、login nサインアップを持っています。 私が望むのは、ログインボタンをクリックすると、私はこのページにフォームが残っています! 歓迎のメッセージを添えてindex.phpだけ。PDOを使用したPHPログインシステム

私は2つのファイルを持っています: 1. index.phpには2つのフォームが含まれています。 2. 2つの関数login.addNewUserを持つuser.php(Userクラス)。

この私のインデックスファイル

<?php 
    $user = new User(); 
    if (isset($_POST['login'])) { 
     $username = $_POST['username']; 
     $password = $_POST['password']; 

     $user->login($username, $password); 
    } 
    if (isset($_POST['signup'])) { 
     $username = $_POST['username']; 
     $password = $_POST['password']; 
     $email = $_POST['email']; 

     $user->add($username, $password, $email); 
    } 
?> 
<form action='' method='post' accept-charset='utf-8'> 
    <input type='text' name='username' placeholder='Username' autofocus=''> 
    <input type='password' name='password' placeholder='Password'> 
    <input type='submit' name='login' value='Login' /> 
</form> 

<form action='' method='post' accept-charset='utf-8'> 
    <input type='text' name='username' placeholder='Username'></br> 
    <input type='email' name='email' placeholder='Email'></br> 
    <input type='password' name='password' placeholder='Password'></br></br> 
    <input type='submit' name='signup' value='Sign Up' /> 
</form> 

そして、これはUser.php

public function login($username, $password){ 
    session_start(); 
    if (!empty($username) && !empty($password)) { 
     $stmt = $this->db->prepare("SELECT * FROM user WHERE username=? and password=?"); 
     $stmt->bindParam(1, $username); 
     $stmt->bindParam(2, $password); 
     $stmt->execute(); 
     if ($stmt->rowCount() == 1) { 
      $_SESSION['login'] = true; 
      $_SESSION['username'] = $username; 
     } else { 
      echo "Wrong username or password"; 
     } 
    } else { 
     echo "Please enter username and password"; 
    } 
} 

の私のログイン機能であるplsは私がこれを成し遂げる助けて!

+0

ハッシュパスワード。 – chris85

答えて

0

フォームは何のログインが行われていない場合にのみ、表示されて、なぜ

<?php if (!$_SESSION['login']) { ?> 
    ... show forms .. 
<?php } ?> 

のようなものを入れていませんか? index.phpの中

0

本のすべて:

<?php 
function login($username, $password){ 
    session_start(); 
    if (!empty($username) && !empty($password)) { 
     $stmt = $this->db->prepare("SELECT * FROM user WHERE username=? and password=?"); 
     $stmt->bindParam(1, $username); 
     $stmt->bindParam(2, $password); 
     $stmt->execute(); 
     if ($stmt->rowCount() == 1) { 
      $_SESSION['login'] = true; 
      $_SESSION['username'] = $username; 
     } else { 
      echo "Wrong username or password"; 
     } 
    } else { 
     echo "Please enter username and password"; 
    } 
} 

$user = new User(); 
if (isset($_POST['login'])) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    $user->login($username, $password); 
} 
elseif (isset($_POST['signup'])) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $email = $_POST['email']; 

    $user->add($username, $password, $email); 
} 
else 
{ 
?> 
<form action='index.php' method='post' accept-charset='utf-8'> 
    <input type='text' name='username' placeholder='Username' autofocus=''> 
    <input type='password' name='password' placeholder='Password'> 
    <input type='submit' name='login' value='Login' /> 
</form> 

<form action='index.php' method='post' accept-charset='utf-8'> 
    <input type='text' name='username' placeholder='Username'></br> 
    <input type='email' name='email' placeholder='Email'></br> 
    <input type='password' name='password' placeholder='Password'></br></br> 
    <input type='submit' name='signup' value='Sign Up' /> 
</form> 
<?php 
} 
?> 
関連する問題