我使用sql创建了一个简单的登录系统
它有4个主要组件索引 - 用户名和传递checklogin - 检查凭据logsuccess主页 - 登录页面成功登录后
产生的错误在帖子的末尾给出
Index.php asks for username and pass
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Nottingham Uni</TITLE>
<script type="text/javascript" src="js/mootools-1.2.1-core-yc.js"></script>
<script type="text/javascript" src="js/process.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</HEAD>
<BODY>
<center>
<div id="intro">
<p> </p>
<p><img align="absmiddle" src="images/nott-uni-logo.jpg"></p>
</div>
<div id="status">
<fieldset><legend align="center">Authentication</legend>
<div id="login_response"><!-- spanner --></div>
<form id="login" name="login" method="post" action="checklogin.php">
<table align="center" width="300" border="0">
<tr>
<td width="80">Username</td><td><input id="name" type="text" name="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="mypassword"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input id="submit" type="submit" name="submit" value="Login">
</tr>
</table>
</form>
</fieldset>
</div>
</center>
</BODY>
</HTML>
checklogin.php checks for the credentials
<?php
$link = mysql_connect('www.xxxxx.com', 'xxxxxx', 'xxxxxx');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("brainoidultrafb", $link);
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM logintbl WHERE stu_email='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
If its success it goes to homepage.php
logsuccess.php is below
<?php
session_start();
if(!session_is_registered(myusername)){
header("location:homepage.php");
}
?>
<html>
<body>
Login Successful
</body>
</html>
these codes are give in the following errors
Deprecated: Function session_register() is deprecated in /home/content/58/9508458/html/pabrowser/checklogin.php on line 29
Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /home/content/58/9508458/html/pabrowser/checklogin.php:29) in /home/content/58/9508458/html/pabrowser/checklogin.php on line 29
Deprecated: Function session_register() is deprecated in /home/content/58/9508458/html/pabrowser/checklogin.php on line 30
Warning: Cannot modify header information - headers already sent by (output started at /home/content/58/9508458/html/pabrowser/checklogin.php:29) in /home/content/58/9508458/html/pabrowser/checklogin.php on line 31
Run Code Online (Sandbox Code Playgroud)
Pee*_*Haa 12
而不是做:
session_register("myusername");
session_register("mypassword");
Run Code Online (Sandbox Code Playgroud)
你可以简单地做:
session_start();
$_SESSION['username'] = 'something';
$_SESSION['password'] = 'something';
Run Code Online (Sandbox Code Playgroud)
要检查是否设置了用户名,您可以执行以下操作:
session_start();
if(!isset($_SESSION['username'])){
// not logged in
}
Run Code Online (Sandbox Code Playgroud)
请注意,我的session_start()功能正好在我的检查/初始化之上.在您的代码中,您可能希望将其添加到脚本的顶部以防止"已经由PHP发送的标头"消息.
另外,请不要使用mysql_*新代码的功能.它们不再维护,社区已开始弃用过程.看到红色的盒子?相反,您应该了解准备好的语句并使用PDO或MySQLi.如果你无法决定,这篇文章将有助于选择.如果你想学习,这是一个很好的PDO教程.
关于你的代码的最后一件事.看起来你没有正确地对密码进行哈希处理,这被认为是不好的做法.如果攻击者掌握了您的数据库,您可以向数据库中的人员做一些解释(例如,您必须告诉他们攻击者获得了所有密码).
| 归档时间: |
|
| 查看次数: |
17571 次 |
| 最近记录: |