Domanda

I am searching for a answer how to create a $_SESSION login.

The result I get every time is Dexter. Even when I just press the login button. I am using sessions and no MySQL or other database. I am just in the beginning to learn PHP and have looked around here and used google but I can't relate was I am doing wrong.

The login page looks like this:

        <?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');
?>

And when logged in:

    <?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');
?>
È stato utile?

Soluzione

You have a better example here: 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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top