用户名/密码的PHP会话数据

时间:2014-02-17 22:31:48

标签: php session post

我使用登录输入创建了一个html文件:

<?php include "http://website.com/login.php"; ?>
<link href="http://website.com/css/login.css" rel="stylesheet" type="text/css" />

<div class="contactForm" id="contactForm" style="width: 500px; margin: auto; float: left;">
   <form name="myform" method="post" action="http://website.com/login.php">
     <p class="form" style="width: 245px; height: 116px; margin-right: 10px;"><input class="field" type="text" name="username" placeholder="Name" />
     <input class="field" type="password" autofocus="" name="passwort" placeholder="Passwort" />
     <button class="button" name="Submit" >Login</button></p>
   </form>
</div>

然后将用户名和密码发送到login.php文件,如下所示:

<?php
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     session_start();

     $username = $_POST['username'];
     $passwort = $_POST['passwort'];

     $hostname = $_SERVER['HTTP_HOST'];
     $path = dirname($_SERVER['PHP_SELF']);

     // username and password are checked
     if ($username == 'admin' && $passwort == 'admin') {
        $_SESSION['angemeldet'] = true;

            // redirect to protected page
            if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
                if (php_sapi_name() == 'cgi') {
                    header('Status: 303 See Other');
                }
                else {
                    header('HTTP/1.1 303 See Other');
                }
            }

       header('Location: downloadarea.php');
       exit;
       }
      }
?>

进入downloadarea.php文件,该文件实际上是一个html文件,只包含一个auth.php文件,用于检查用户是否已登录。当用户未登录时,它将重定向到登录html文件(而不是login.php)。

downloadarea.php:

<?php include('auth.php'); ?>

auth.php:

<?php
     session_start();

     $hostname = $_SERVER['HTTP_HOST'];
     $path = dirname($_SERVER['PHP_SELF']);

     if (!isset($_SESSION['angemeldet']) || !$_SESSION['angemeldet']) {
      header('Location: website.com/login.html/');
      exit;
      }
?>

确定。现在我想要一个小小的消息出现(一种红色边框,或一条消息,显示在我的登录按钮下面)。但是,我不知道怎么做,因为login.php需要将变量发送回login.html,我有输入。我需要什么代码?

编辑:login.php,auth.php和downloadarea.php与我的input-html文件不在同一个目录中

提前致谢

2 个答案:

答案 0 :(得分:1)

修改

<强>的login.php

<?php
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     session_start();

     $username = $_POST['username'];
     $passwort = $_POST['passwort'];

     $hostname = $_SERVER['HTTP_HOST'];
     $path = dirname($_SERVER['PHP_SELF']);

     // username and password are checked
     if ($username == 'admin' && $passwort == 'admin') {
        $_SESSION['angemeldet'] = true;

            // redirect to protected page
            if ($_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.1') {
                if (php_sapi_name() == 'cgi') {
                    header('Status: 303 See Other');
                }
                else {
                    header('HTTP/1.1 303 See Other');
                }
            }

       header('Location: downloadarea.php');
       exit;
       }

        else {
        echo "<div id='session_name'>Wrong credentials</div>";
        }
?>

<!DOCTYPE html>
<head>
<style>
#session_name {
font-weight:bold;
color:#0000FF;
border-style:solid;
border-width:2px;
border-color: #FF0000;
background:#CCCCCC;
}
</style>
</head>
<body>
</body>
</html>

原始答案

  

“现在我想要一条小信息出现(有点红色边框,或者是一条信息,显示在我的登录按钮下面)”

这是一个非常基本的方法,使用USER作为会话名称:

<?php
session_start();
$_SESSION['angemeldet'] = "USER";
?>

<?php
if (isset($_SESSION['angemeldet']) || !empty($_SESSION['angemeldet'])) { 
echo "You are logged in." . $_SESSION['angemeldet'];
}
?>

<br><br>
Welcome <?php echo $_SESSION['angemeldet']; ?>

添加样式:(基本示例)

<div id="session_name">Welcome <?php echo $_SESSION['angemeldet']; ?></div>

<style>
#session_name {
font-weight:bold;
color:#0000FF;
border-style:solid;
border-width:2px;
border-color: #FF0000;
background:#CCCCCC;
}
</style>

答案 1 :(得分:0)

如果我理解正确,你可以使用login.html将所有内容发送到login.php,然后检查login.php中的所有内容。之后你想回到login.html并在错误时显示一条消息。

所以要做的是使用您在login.html中创建的会话:

<?php
session_start();
if(isset($_SESSION['angemeldet'])){
    //Something you want to happen when you are logged in.
}
else{
     echo "Wrong username/password";
}
?>

红色边框等应该在css中制作。