0

I cant seem to get this working properly, i've tried several things like

echo $_SESSION['Username'];
echo $_POST['Username'];

Im a beginner with this so not sure how to solve it.

Here is my full code of login.php:

<?php session_start(); /* Sessionen påbörjas här */ 

/* Submit för formuläret */ 

if(isset($_POST['Submit'])){

    /* Tillgängliga lösenord och användarnamn */

    $logins = array('Mattias' => '1','Mikael' => '2',);

    /* Kolla och ge lösenord och användarnamn en ny variabel */

    $Username = isset($_POST['Username']) ? $_POST['Username']:' ';
    $Password = isset($_POST['Password']) ? $_POST['Password']:' ';

    /* Kolla lösenord och användarnamn */

    if (isset($logins[$Username]) == $Password){

        /* Vid lyckat försök skicka vidare till den lösenordsskyddade sidan  */

        $_SESSION['UserData']['Username']=$logins[$Username];       
        header("Location:../admin/index.php");
        exit(); 
    } else {

        /* Meddelande för fel lösenord/användarnamn */

        $felmeddelande = "<span style='color:red;'>Fel användarnamn    eller lösenord</span>";
    }
}
?>
<?php include 'config.php';?>
<?php $page_title = "Moment 2"; ?>
<?php include 'header.php';?>
    <html>
    <body>
    <div class="wrapper">

    <form action="" method="post" name="Login_Form">
    <?php if(isset($felmeddelande)){?>
    <?php echo $felmeddelande;?>
    <?php } ?>
    <br>
    <h3>Logga in här:</h3>
    <p style="font-size:0.8em;font-family:sans-serif;"><em>Ps.. loginuppgifter för mattias: Mattias / 1 <br>
        För Mikael: Mikael / 2</em></p>
    Användarnamn<br>
    <input name="Username" type="text" class="Input"><br><br>
     Lösenord<br>
    <input name="Password" type="password" class="Input"><br><br>
    <input name="Submit" type="submit" value="Logga in"><br><br>
    </form>

   </div>
 </body>
</html>

<?php include 'foot.php';?>

*Notice the comments etc. is in swedish.

here is admin/index.php:

<?php 
session_start();

if(!isset($_SESSION['UserData']['Username'])){
header("location:../includes/login.php");
exit;
}
?>
<!-- Lägger till alla php-filer. -->
<?php include '../includes/config.php'; ?>
<?php $page_title = "Admin"; ?>
<?php include 'adminheader.php';?>

<div class="wrapper">
<p style="margin-top:-0px; padding-top:30px; padding-bottom:40px; margin-bottom:-0px">Hej<?php echo $Username;?>, du har kommit till den lösenordskyddade sidan! </p>
</div>

<?php include '../includes/foot.php'; ?>

*Updated like this:

        $Username = $_SESSION['UserData']['Username']=$Username;
    /* Kolla och ge lösenord och användarnamn en ny variabel */
    $Anvandare = isset($_POST['Username']) ? $_POST['Username']:' ';
    $Losen = isset($_POST['Password']) ? $_POST['Password']:' ';
    /////////////////////////////////////
    /* Kolla lösenord och användarnamn */
    /////////////////////////////////////       
    if (isset($Username[$Anvandare]) == $Losen){
        ///////////////////////////////////////////////////////////////////////
        /* Vid lyckat försök skicka vidare till den lösenordsskyddade sidan  */
        ///////////////////////////////////////////////////////////////////////
        $_SESSION['UserData']['Username']=$Username[$Anvandare];    

and

<?php 
session_start();

if(!isset($_SESSION['UserData']['Username'])){
header("location:../includes/login.php");
exit;
}
?>
  • 1
    $_SESSION['UserData']['Username']=$logins[$Username]; here you set the session array element to your password, not username! – clemens321 Nov 22 '15 at 21:21
  • You never defined $Username, you only have your $_SESSION array. Either use the long form or assign $Username = $_SESSION['UserData']['Username']. – clemens321 Nov 22 '15 at 21:22
  • Tried to assign $Username like you did before. But when i do this the 'echo $Username' only displays the password instead of the username. – Douglas Pettersson Nov 22 '15 at 21:26
  • See my first comment, you assign it wrong in your login.php – clemens321 Nov 22 '15 at 21:28
  • I must be stupid but i can't seem to get this right. When i assign $Username as you did and remove the old $Username variable the script stops working. – Douglas Pettersson Nov 22 '15 at 21:36
  • In login.php remove $logins[] array, only $Username: $_SESSION['UserData']['Username']=$Username; - in admin/index.php vice versa. – clemens321 Nov 22 '15 at 21:42
  • Now it looks like this and still does not work. Edited the question. – Douglas Pettersson Nov 22 '15 at 21:50
  • http://pastebin.com/029ACyma - based your first files, see the two "Change"-comments. If this doesn't work read the marked duplicate article and use var_dump() a dozen times. – clemens321 Nov 22 '15 at 22:09

0 Answers0