2016-10-06 10 views
-2

私はphpに新しいですし、私は登録し、ページにログインしようとしています。Loggin in PHPが動作しません

私は新しいユーザーとして登録すると正常に動作し、データベースに表示されます。しかし、ログインする際には、私はすべてを試した問題があるようです。

私がしたいのは、ユーザーがログインしてホームページにリダイレクトするときです。ログイン情報が間違っていると、エラーメッセージが表示されます。ここで

は、ファイル内のログにあるPHPコードは次のとおりです。

<?php 

session_start(); 

if(isset($_SESSION['users_id']) ){ 
    header("Location: /"); 
} 

require 'database.php'; 

if(!empty($_POST['email']) && !empty($_POST['password'])): 

    $records = $conn->prepare('SELECT id,email,password FROM users WHERE email = :email'); 
    $records->bindParam(':email', $_POST['email']); 
    $records->execute(); 
    $results = $records->fetch(PDO::FETCH_ASSOC); 

    $message = ''; 

    if(count($results) > 0 && password_verify($_POST['password'], $results['password']) ) 
    { 
     $_SESSION['users_id'] = $results['id']; 
     header("Location: php.dev/index.php", true, 301); exit(); 
    } 
    else { 
     $message = 'Sorry, thoes credentials do not match'; 
    } 

endif; 

?> 

header("Location:....)これが動作するようには思えません。私は本当にここに何か助けてくれる?ここで

はhtmlコードは次のとおりです。

<!doctype html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Login</title> 
<link href="Style/phpstyle.css" rel="stylesheet" type="text/css"> 
<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"> 

</head> 

<body> 

<div class="header"> 
    <a href="php.dev/index.php"> TIPBUCKET </a> 
</div> 

<?php if(!empty($message)): ?> 
    <p><?= $message ?></p> 
<?php endif; ?> 

<h1>Login</h1> 

<span> or <a href="register.php">Register here</a></span> 

<form action="login.php" method="POST"> 

<input type="text" placeholder="enter your email" name="email"> 

<input type="password" placeholder="Password" name="password"> 

<input type="submit"> 

</form> 

</body> 
</html> 

This is what happens when i log in

は、任意の回答に事前にありがとう:)

+0

'php.dev/index.php'とは何ですか?確かにそれはURLではありません... – arkascha

+0

私はローカルホストMAMPを使用していますので、現在のURLはphp.devでindex.phpはホームページです – user3719086

+2

有効なURLは 'http://php.dev/ index.php'。 'php.dev/index.php'はドットとスラッシュを含む任意の文字列です。 – arkascha

答えて

0

あなたのヘッダーにタイプミスがあります

header("Location: php.dev/index.php", ture, 301); 

header("Location: php.dev/index.php", true, 301); 
+0

良い目。しかし、それでも同じことが起こります – user3719086

+0

エラーメッセージが表示されますか? – BananasSplitter

+0

"if {count($ results)............}"をスキップして "else {$ message ......}"行に移動し、申し訳ありません、資格情報が一致しません ' – user3719086

関連する問題