PHP/MySQL登录 [英] PHP/MySQL login

查看:65
本文介绍了PHP/MySQL登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建简单的登录表单.

I'm trying to create simple login form.

login.php

<?php

include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
           $myusername=addslashes($_POST['username']); 
$mypassword=addslashes($_POST['password']); 


$sql="SELECT id FROM login_test WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];

$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;
//echo $_SESSION['login_user'];
header("location: welcome.php");
}
else 
{
$error="Your Login Name or Password is invalid";
 }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.    dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Page</title>

<style type="text/css">
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;

}
label
{
font-weight:bold;

width:100px;
font-size:14px;

}
.box
{
border:#666666 solid 1px;

}
</style>
</head>
<body bgcolor="#FFFFFF">

<div style="font-weight:bold; margin-bottom:10px">Demo Login Details -> Username : <a href="#">test</a>  Password : <a         href="#">test</a></div>

<div align="center">
<div style="width:300px; border: solid 1px #333333; " align="left">
<div style="background-color:#333333; color:#FFFFFF; padding:3px;"><b>Login</b></div>


 <div style="margin:30px">

<form action="" method="post">
<label>UserName  :</label><input type="text" name="username" class="box"/><br /><br />
<label>Password  :</label><input type="password" name="password" class="box" /><br/><br />
<input type="submit" value=" Submit "/><br />

</form>
<div style="font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>
</div>
</div>
</div>

</body>
</html>

lock.php

<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];
$ses_sql=mysql_query("SELECT username FROM login_test WHERE username='$user_check' ");

$row=mysql_fetch_array($ses_sql);

$login_session=$row['username'];

if(!isset($login_session))
{
header("Location: login.php");
}
?>

welcome.php

<?php

include('lock.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.   dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome </title>
</head>

<body>
<h1>Welcome <?php echo $login_session; ?></h1>
 <h2>Login succesfull</h2>

<h2><a href="logout.php">Sign Out</a></h2>
</body>
</html>

问题是它不断重定向回login.php,lock.php出现了,我不确定这是怎么回事 输入错误的用户名密码后,它会正确显示消息

Problem is it keeps redirecting back to login.php, lock.php kicks in, I'm not sure what is wrong When I input wrong username password, it displays message correctly

推荐答案

问题在这里

if($count==1)
{
    session_register("myusername");
    ^ // this is deprecated
    $_SESSION['login_user']=$myusername;
    //echo $_SESSION['login_user'];
    header("location: welcome.php");
}

请勿使用session_register它已弃用

通过使用此代码直接使用

use it directly by using this code

$_SESSION['login_user']="myusername";

这篇关于PHP/MySQL登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆