0
我應該使用基本的PHP,HTML和MySQL作爲學校作業。我該怎麼做才能使用我的代碼進行註冊或登錄?
我在線學習了一個教程,並更改了代碼的各個方面,但我無法「註冊」或「登錄」,因此我可以查看其他頁面。
這是我的索引頁。
<?php
session_start();
include_once '../db_connect.php';
//starts the session
if(isset($_SESSION['login'])!="") {
header("Location: home.php");
}
if(isset($_POST['btn-login'])) {
$email = mysqli_real_escape_string($_POST['email']);
$upass = mysqli_real_escape_string($_POST['upass']);
$res = mysqli_query("SELECT * FROM users WHERE email='$email'");
$row = mysqli_fetch_array($res);
if($row['password']==md5($upass)) {
$_SESSION['user'] = $row['user_id'];
header("Location: home.php");
} else {
echo "<p>You entered in the wrong information. Please re-enter.</p>\n";
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<main>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="upass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-login">Sign In</button></td>
</tr>
<tr>
<td><a href="register.php">Sign Up Here</a></td>
</tr>
</table>
</form>
</div>
</main>
</body>
</html>
這裏是我的註冊頁面:
<?php
session_start();
if(isset($_SESSION['login'])!="") {
header("Location: home.php");
}
include_once '../db_connect.php';
if(isset($_POST['btn-signup'])) {
$name = mysqli_real_escape_string($_POST['name']);
$uname = mysqli_real_escape_string($_POST['uname']);
$email = mysqli_real_escape_string($_POST['email']);
$upass = mysqli_real_escape_string($_POST['upass']);
if(mysqli_query("INSERT INTO users(name, uname, email, upass) VALUES('$name', $uname', '$email', '$upass')")) {
echo "<p>Thank you $uname for registering with us!\n</p>";
} else {
echo "<p>Could not open a connection to the mySQL server due to error: <strong>" . mysqli_connect_error($db_connect)."</strong></p>";
echo "<p>Sorry Guest, but you were unable to register. Please try again later.\n</p>";
} //end mysql_query if statement
} //end btn-signup if statement
?>
<!doctype html>
<html xmlns="http://www.3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Login and Registration System</title>
</head>
<body>
<main>
<div id="login-form">
<form method="post">
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="name" placeholder="First Name" required /></td>
</tr>
<tr>
<td><input type="text" name="uname" placeholder="User Name" required /></td>
</tr>
<tr>
<td><input type="email" name="email" placeholder="Your Email" required /></td>
</tr>
<tr>
<td><input type="password" name="upass" placeholder="Your Password" required /></td>
</tr>
<tr>
<td><button type="submit" name="btn-signup">Sign Me Up</button></td>
</tr>
<tr>
<td><a href="index.php">Sign In Here</a></td>
</tr>
</table>
</form>
</div>
</main>
</body>
</html>
這裏是主頁:
<?php
session_start();
include_once '../db_connect.php';
//starts the session
if(!isset($_SESSION['login'])) {
header("Location: index.php");
}
$res=mysqli_query("SELECT * FROM users WHERE user_id=".$_SESSION['login']);
$userRow=mysqli_fetch_array($res);
?>
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>WELCOME! <?php echo $userROW['email']; ?></title>
</head>
<body>
<div id="header">
<div id="left">
<label>cleartuts</label>
</div>
<div id="right">
<div id="content">
hi' <?php echo $userRow['uname']; ?> <a href="logout.php?logout">Sign Out</a>
</div>
</div>
</div>
</body>
</html>
登出頁面:
<?php
session_start();
if(!isset($_SESSION['login'])) {
header("Location: index.php");
} else if (isset($_SESSION['user'])!="") {
header("Location: home.php");
}
if(isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['login']);
header("Location: index.php");
}
?>
最後是db_connect腳本:
<?php
//defining constants
//NOTe: once you upload your site to the Internet, you must create a proper user (never use root)
//with a secure password, what follows is only for your test site:
DEFINE ('DB_USER', 'root'); //username to access the mySQL server
DEFINE ('DB_PASSWORD', 'root'); //password for that username
DEFINE ('DB_HOST', 'localhost'); //in most situations your host will be 'localhost'
DEFINE ('DB_NAME', 'login'); //this the the name of the database you want ot connect to
//connecting to the mySQL server
$db_connect = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!empty($db_connect)) {//see if we connected successfully
//if we opened a connection to the mySQL server,
//check to see if we can access the database
if(!mysqli_select_db ($db_connect, DB_NAME)) {
//if we can NOT access the database give out an error
//debugging error, comment out when site goes live
echo "<p>Error Number: <strong>". mysqli_errno($db_connect)."</strong>. Could not select the database: <strong>". DB_NAME."</strong>, due to error: <strong>". mysqli_error($db_connect)."</strong></p>\n";
//user friendly error message with no technical details
echo "<p>The site is currently experiencing technical difficulties. We apologize for any inconvenience. Please try again later.</p>\n";
//if you have a footer include for your site, uncomment this
//include_once ('includes/footer.html');
exit(); //exits the script
} // end if !mysqli_select_db (DB_NAME)
} else { //could not connect to the mySQL server
//debugging error, comment out when site goes live
echo "<p>Could not open a connection to the mySQL server due to error: <strong>" . mysqli_connect_error($db_connect)."</strong></p>";
//user friendly error message with no technical details
echo "<p>The site is currently experiencing technical difficulties. We apologize for any inconvenience. Please try afain later.</p>\n";
//if you have a footer include for your site, uncomment this
//include_once ('includes/footer.html');
exit(); //exit the script
} //and else statement
//if all goes well then the page that called this connection script will
//continue to run, if not, one of the exit statements above stopped it.
?>
我不知道爲什麼它不會讓我登陸...幫助?
它看起來好像你沒有設置$ _SESSION ['login']的值(我可能錯過了你提供的一些代碼)。 – jeff
「* Not working *」非常含糊,可能想要更具體一些。 – Qirel
您的表中的密碼列很小(20個字符),如果可以的話,可以像'varchar(255)'一樣提升它,您可能會切斷密碼。另外,看看你的PHP版本是否會使用'password_hash()'來存儲密碼。如果是這樣,請使用該函數進行存儲,然後使用'password_verify()'進行檢查 – Rasclatt