使用会话和多个用户创建简单的PHP登录

时间:2014-05-13 12:23:52

标签: php session login-script

我正在寻找如何创建$ _SESSION登录的答案。

每次得到的结果都是德克斯特。即使我只是按下登录按钮。我正在使用会话,没有MySQL或其他数据库。我刚刚开始学习PHP并且已经在这里查看并使用谷歌,但我无法理解我是做错了。

登录页面如下所示:

        <?php 
        session_start();
        $_SESSION['usernamne1'] = "Dexter"; 
        $_SESSION['usernamne2'] = "River";
        $_SESSION['usernamne3'] = "Miro";

        $_SESSION['password1'] = "meow1";
        $_SESSION['password2'] = "meow2";
        $_SESSION['password3'] = "meow3";
        ?>
<?php //Header  include fil
$page_title = 'Login'; //Dynamic titel 
include('includes/header.html');
?> 
<?php 
echo "<h3>Login!</h3>";
echo "<br />";
?>

<form method="post" Action="sida7logged.php">

<fieldset><legend>Fyll i dina användaruppgifter</legend>

<p><label>Username: <br />
<input name="usernamne" type="text"></label></p>
<p><label>Password: <br />
<input name="password" type="password"></label></p>

<input type="Submit" value="Login">
    </fieldset>
    </form> 


<?php //Footer include file 
include('includes/footer.html');
?>

登录后:

    <?php 
$page_title = 'Logged in'; //Dynamisc title 
include('includes/header.html');
?> 
<?php 
session_start();

if($_SESSION['usernamne1']==true || ($_POST['username']=="Dexter" 
&& ($_SESSION['password1']==true || $_POST['password']="meow1"))) {
$_SESSION['usernamne1']=true;
echo "Hello Dexter";
}

elseif($_SESSION['usernamne2']==true || ($_POST['username']=="River" 
&& ($_SESSION['password2']==true || $_POST['password']="meow2"))) {
$_SESSION['usernamne2']=true;
echo "Hello River";
}

elseif($_SESSION['usernamne3']==true || ($_POST['username']=="Miro" 
&& ($_SESSION['password3']==true || $_POST['password']="meow3"))) {
$_SESSION['usernamne1']=true;
echo "Hello Miro";
}

else {
echo "Please login";
}
?>

<?php //Footer include file 
include('includes/footer.html');
?>

1 个答案:

答案 0 :(得分:1)

这里有一个更好的例子:Easy login script without database

<?php
session_start();

$userinfo = array(
                'user1'=>'password1',
                'user2'=>'password2'
                );

if(isset($_GET['logout'])) {
    $_SESSION['username'] = '';
    header('Location:  ' . $_SERVER['PHP_SELF']);
}

if(isset($_POST['username'])) {
    if($userinfo[$_POST['username']] == $_POST['password']) {
        $_SESSION['username'] = $_POST['username'];
    }else {
        //Invalid Login
    }
}
?>
<!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>
        <title>Login</title>
    </head>
    <body>
        <?php if($_SESSION['username']): ?>
            <p>You are logged in as <?=$_SESSION['username']?></p>
            <p><a href="?logout=1">Logout</a></p>
        <?php endif; ?>
        <form name="login" action="" method="post">
            Username:  <input type="text" name="username" value="" /><br />
            Password:  <input type="password" name="password" value="" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>