具有会话的概念PHP和HTML

时间:2015-10-21 12:30:32

标签: php html session slim

我是使用php,html和构建网站的新手,我开始关注Sessions的工作方式等等。我只是试图在网站上使用sessions,mysql和slim设置login/logout来提供一些背景

如果不允许在客户端运行php,我对如何正确使用会话感到困惑。根据我的理解,当你进行post api调用登录时,你会做什么,如果成功,你会将$_SESSION['userLogin']值设置为某个值,以显示用户已登录。然后在主页文件上( index.php),如果设置了$_SESSION['userLogin'],你将进行php测试。如果是,则退出"退出"按钮将显示。如果没有设置,请登录"按钮将显示。我已经输入了我正在谈论的内容。这不起作用,因为当我的浏览器加载页面时,PHP被注释掉了。

有人可以向我解释一下吗?如果您不能使用php客户端,那么您应该知道是否设置了$ _SESSION [' userLogin']以及要加载的html?

代码:

<!DOCTYPE html>
<html>
<head>
<div id="content_area">
    <div id="features">
        <table>
            <tr>
                <td><img class="feature-icon" src="../_images/_icons/marshall-to-hunters.png"/></td>
                <td class="feature-text">Connect to a large base of bug hunters.</td>
            </tr>
            <tr>
                <td><img class="feature-icon" src="../_images/_icons/get-paid.png"></td>
                <td class="feature-text">Get paid for finding bugs.</td>
            </tr>
        </table>
    </div> 
<?php
if(!isset($_SESSION['userLogin'])
    {
      echo '
      <form id="login" name="loginForm" method="post">
      <input class="loginForm" type="submit" value="Log In" id="submitLogin">
      <input class="loginForm" type="password" name="password" placeholder="Password" id="passLogin">
      <input class="loginForm" type="text" name="username" placeholder="Username" id="usernameLogin">
    </form>';
    }
    else 
    {
      //Sometype of logout button
    }
    ?>
</div>

1 个答案:

答案 0 :(得分:1)

In your code your saying is the $_SESSIOn['userLogin'] is set show the user a login form:

<?php
if(isset($_SESSION['userLogin'])
{
  echo '
  <form id="login" name="loginForm" method="post">
  <input class="loginForm" type="submit" value="Log In" id="submitLogin">
  <input class="loginForm" type="password" name="password" placeholder="Password" id="passLogin">
  <input class="loginForm" type="text" name="username" placeholder="Username" id="usernameLogin">
</form>';
}
else 
{
  //Sometype of logout button
}
?>

if the $_SESSION['userLogin'] is set this means the user is logged in, so where your echoing out the form, you need to echo the logout button and kill the session.

To keep the code you have you could use if(!isset(.....

Also, as mentioned above in a comment, you need to include session_start() on all pages, you might be better off creating one file and including it in all the pages where you need to use the session code.