登录页面,在同一页面查看密码

时间:2013-12-23 18:04:09

标签: php mysql login header

我试图创建一个登录页面,但我无法使其正常工作。我有三个文件:标题,登录和索引页面。以下是代码: admin_header.php:

<html>
    <head>
        <title>Project</title>
<link type="text/css" rel="stylesheet" href="styles.css"/>
    </head>
    <body>
    <body id="home">
<div class="top" id="header">
<ul>

        <li><a class="menu" href="index.php">Home</a></li>
        <li><a class="menu" href="search.php">Add an Artist</a></li>
        <li><a class="menu" href="contact.php">List the Artists</a></li>
        <li><a class="menu" href="contact.php">Add a movie</a></li>
        <li><a class="menu" href="contact.php">List the movies</a></li>
        <li><a class="menu" href="contact.php">Add an admin</a></li>
</ul>


</div>
<?php
 $server = 'localhost';
 $user = 'root';
 $pass = 'blablabla';
 $mydb = 'projectdb';
 $movies = 'movies';
 $artists='artists';
 $roll='roll';
 $users='Users';
 $connect = mysqli_connect($server, $user, $pass);
 mysqli_select_db($connect,$mydb)or die("cannot select DB");
session_start();
define('ADMIN',$_SESSION['username']);
if(!$_SESSION['username'])
{
header("location:admin_login.php");
}

?>
</body></html>

admin_login:

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
<?php

    $login_form = <<<EOD
<form name="login" id="login" method="POST" action="">
<p><label for="username">Please Enter Username: </label><input type="text" size="100" name="username" id="username" value="" /></p>
<p><label for="password">Please Enter Password: </label><input type="password" size="40" name="password" id="password" value="" /></p>
<p><input type="submit" name="submit" id="submit" value="Submit"/> <input type="reset" name="reset" id="reset" value="reset"/></p>
</form>
EOD;
if (isset($_GET['msg']))
{
$msg=$_GET['msg'];
if($msg!='') 
{
echo '<p>'.$msg.'</p>';
}
}
 //If message is set echo it}

echo "<h1>Please enter your Login Information</h1>";
echo $login_form;
ob_start();


       if (!isset($_POST['username'])||($_POST['password']))
       {
       echo "Please, enter your login information";
       }
       else
       {
       $username = $_POST['username']; 
       $password = $_POST['password'];
 if (!$connect)
       {
       echo ("Cannot connect to $server using $user");
       }
       $username = stripslashes($username);
       $password = stripslashes($password);
       $username = mysqli_real_escape_string($connect,$username);
       $password = mysqli_real_escape_string($connect,$password);
       $sql="SELECT * FROM $users WHERE username='".$username."' and password='".$password."'";
       $result=mysqli_query($connect,$sql);
       $count=mysqli_num_rows($result);
       if($count==1)
       {
       session_start();
       $_SESSION["username"]=$username;
       $_SESSION["password"]=$username; 
       header("location:admin_index.php");

       }
       else 
       { 
        $msg = "Wrong Username or Password. Please retry";
        echo $msg;
       }
       }
ob_end_flush();
?>
</body>
</html>

admin_index:

<html>
<body>
<?php
include('admin_header.php');
?>

<html>
<body>
Login Successful
</body>
</html>

我没有收到任何错误,但无论我输入什么数据,我都不会被重定向到索引页面。你能帮忙吗?

3 个答案:

答案 0 :(得分:1)

一些问题:

  • 当您已将文本输出到浏览器时,您正在使用header()来电。这不起作用,在将任何内容输出到浏览器之前,你需要将所有PHP内容(如session_startheader调用)置于脚本的最顶层。
  • 永远不要使用纯文本密码,你应该在存储之前对它们进行加盐和散列。

答案 1 :(得分:0)

首先,我没有看到您要将数据提交到的页面。基本思路是,有一个接受输入的.html或.php文件,然后将这些输入值发送到另一个页面,以便进一步处理,包括验证和进入数据库等。 一个例子:

User.html

<form name="login" id="login" method="POST" action="authenticate.php">
<input type="text" name="username" />
<input type="password" name="passwrd"/>
<input type="submit" name="Login" value="login"/>
</form>

然后在Authenticate.php上

<?php
if(isset($_POST['Login'])){
$uname=$_POST['uname'];
$pass= $_POST['passwrd'];
// from here on.. you have to also validate the inputs, which could be done with filter_var and then use a password library like Bcrypt available on Github before you send your data to database.
}
?>

其次,尝试分离模型,控制器和视图..不要混合和匹配php和html,它只是更难调试的东西。要打印错误消息,您可以通过json数据发送错误消息。

第三,PHP手册,Google和Youtube很适合您了解基础知识。

答案 2 :(得分:0)

<body>中有一个双admin_header.php标记。

替换

...
<link type="text/css" rel="stylesheet" href="styles.css"/>
    </head>
    <body>
    <body id="home">
<div class="top" id="header">
<ul>
...

使用

    ...
    <link type="text/css" rel="stylesheet" href="styles.css"/>
        </head>
        <body id="home">
    <div class="top" id="header">
    <ul>
    ...

可能无关紧要。

相关问题