0

我是 php 新手。这是检查用户是否使用会话登录然后允许用户的代码。

验证

<?php

session_start();

$uname = $_POST['uname'];
$pass = $_POST['pass'];

if($uname  == "admin"  &&  $pass == "admin")
{
    $_SESSION['uname'] = $uname;
    $_SESSION['auth'] = 1;
    echo "Welcome Mr ".$uname.". You are now logged in ";
    echo "<br>";
    echo "<a href='TakeMeHome.html'>Click here to access the application </a>";
}
else
{
    echo "Invalid username or password";
}

?>

<?php

session_start();


if($_SESSION['auth'] != 1)
{
    echo "You are not logged in! ";
    echo "<a href = \"TakeMeHome.html\">";
    echo "Access Application";
    echo  "</a>";
    exit();
}

?>

<html>
You are now logged in
</html>

但是链接标签正在显示

"; echo "Access Application"; echo ""; exit(); } ?> 

连同html数据。没有进行验证。我知道有很多更好的方法来验证用户是否登录。但我正在学习课程,因此我正在使用课程。你能告诉我哪里出错了吗?
问候。

4

2 回答 2

1

在您的回声代码中使用单引号,如下所示:

<html>
  <head>
  </head>
  <body>
    <?php
      echo "<a href='pageToGoTo.html' title='Page to go to' class='whatEver'>Anchor text</a>";
    ?>
  </body>
</html>

已经告诉了,html应该放在正文中......

于 2013-03-13T14:14:21.503 回答
0

我不知道为什么@Andy 建议你把你的 PHP 放在你的 head 标签中——它不是 javascript。您有 2 种方法可以格式化您的 PHP 和 HTML,第一种是将所有 PHP 放在您的开始 html 标记之上,就像这样

<?php
    session_start();
    if($_SESSION['auth'] != 1) {
      $message = 'You are not logged in! <a href="TakeMeHome.html">Access Application</a>';
    } else {
      $message = 'You are logged in!';
    }
?>
<html>
  <head>      
  </head>
  <body>
    <?php echo $message; ?>
  </body>
</html>

或者,将它放在页面的正文中,如下所示:

<?php
    session_start();
?>
<html>
  <head>      
  </head>
  <body>
    <?php
      if($_SESSION['auth'] != 1) {
        echo 'You are not logged in! <a href="TakeMeHome.html">Access Application</a>';
      } else {
        echo 'You are logged in!';
      }
    ?>
  </body>
</html>

如果您仍然没有得到想要的结果,请使用var_dump($_SESSION);打印出您的会话数组并确保它包含正确的信息。

于 2013-03-13T18:21:41.957 回答