ウェブからテンプレートを入手し、データベースに従って編集しました。私はxamppを使ってこれをテストしています。私はregister.phpに登録できますが、ログインすることはできません。私はlogin.phpのコードに何か間違っていると思います。誰かが問題を指摘できますか? ありがとうございます。ログインできません。以下のコードで何が間違っていますか?
register.php
<?php
require('config.php');
$error="";
if (isset($_POST['submit'])=="Sign up") {
if(!$_POST['name']) $error.="<br/>Please enter your name";
if (!$_POST['email']) $error.="<br/>Please enter your email";
else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.=" <br/>Please enter a valid email address" ;
if (!$_POST['password']) $error.="<br/>Please enter your password";
else{
if(strlen($_POST['password'])<8) $error.= "<br/>Please enter a password with atleast 8 charachters";
if (!preg_match('`[A-Z]`', $_POST["password"])) $error.="<br/>Please include atleast one capital letter in your password";
}
if ($error) echo "There were error(s) in your signup details:" .$error;
else{
$query= "SELECT * FROM `members` WHERE email='".mysqli_real_escape_string($link,$_POST['email'])."'";
$match= mysqli_query($link,$query);
$results= mysqli_num_rows($match);
if ($results) echo "That email address is already registered. Do you want to log in ?";
else {
$name = mysqli_real_escape_string($link,$_POST['name']);
$email = mysqli_real_escape_string($link,$_POST['email']);
$password = password_hash($_POST['password'],PASSWORD_DEFAULT);
$query= "INSERT INTO `members`(name,email,password) VALUES('$name','$email','".$password."')";
$result = mysqli_query($link,$query);
if($result){
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}
}
}
else{
?>
<form method="post">
<h1>Registration</h1><br>
<label for="name">Name</label>
<input type="text" name="name" /><br>
<label for="email">Email</label>
<input type="email" name="email" id="email" /><br>
<label for="password">Password</label>
<input type="password" name="password" /><br>
<input type="submit" name="submit" value="Sign up" /><br>
</form>
<?php } ?>
login.php
<?php
require("config.php");
session_start();
if (isset($_POST['submit'])=="Login"){
$name = mysqli_real_escape_string($link,$_POST['name']);
$password = password_hash($_POST['password'],PASSWORD_DEFAULT);
$query= "SELECT * FROM `members` WHERE name='$name' AND password='".$password."'";
$result= mysqli_query($link,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if ($row = mysqli_fetch_array($result)) {
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
header("Location: profile.php");
}else{
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}
else{
?>
<form method="post">
<label for="name">Name</label>
<input type="name" name="name" /><br>
<label for="password">Password</label>
<input type="password" name="password" /><br>
<input type="submit" name="submit" value="Login" />
</form>
<p>Not registered yet? <a href='register.php'>Register Here</a></p>
<?php } ?>
UPDATE
<?php
session_start();
require("config.php");
if (isset($_POST['submit'])){
print_r($_POST);
$name = mysqli_real_escape_string($link,$_POST['name']);
$password = md5($_POST['password']);
echo $password;
echo $name;
$query = "SELECT * FROM members WHERE name='shohan' AND password='f50db83c94e1e725476295c6ee97d4b8'";
$result = mysqli_query($link, $query) or die(mysqli_error($link)) ;
$rows = mysqli_num_rows($result);
echo($rows);
if ($rows) {
while ($row = mysqli_fetch_row($result)){
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
mysqli_free_result($result);
break;
}
header("Location: profile.php");
exit;
}else{
echo "<div class='form'>
<h3>Username/password is incorrect.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
}
}
?>
<form method="post">
<label for="name">Name</label>
<input type="name" name="name" /><br>
<label for="password">Password</label>
<input type="password" name="password" /><br>
<input type="submit" name="submit" value="Login" />
</form>
<p>Not registered yet? <a href='register.php'>Register Here</a></p>
どうしたらいいのでしょうか?何らかのエラーがありますか? – kerv
無効なユーザー名とパスワードが表示されます。私は登録に使ったのと同じものを入力していますが。 – Syed
mysqlエラーをmysql_error()でチェックしてください。また、この 'isset($ _ POST ['submit'])=="ログイン "' – devpro