Skip to content
Advertisement

Session-Handling doesn’t set properly on PHP

I’m working on php-Website for my School-Project which I have to get finished soon. Now I’m stuck with my Session-Handling. User signup & login works just fine. But this variable isset($_SESSION[“useruid”]) is always false and doesn’t let me access the homepage, even the alert in the header says, that I am successfully logged in.

Here are the two parts, which I think the error might be:

functions.inc.php

function loginUser($connection, $username, $pwd){

    $uidExists = uidExists($connection, $username);

    if ($uidExists === false) {
        header("location: ../login.php?error=wronglogin");
        exit();
    }

    $pwdHashed = $uidExists["usersPwd"];
    $checkPwd = password_verify($pwd, $pwdHashed);

    if ($checkPwd === false) {
        header("location: ../login.php?error=wronglogin");
        exit();
    }
    else if ($checkPwd === true) {
        session_start();
        $_SESSION["userid"] = $uidExists["usersId"];
        $_SESSION["useruid"] = $uidExists["usersUid"];
        header("location: ../index.php?error=loginsuccessful");
        exit();
    }
}

home.php

<?php
  include_once 'header.php';

  if (is_null($_SESSION["useruid"])) {
      header("location: index.php");
      exit();
  }
?>

<button>
    <?php
        if (isset($_SESSION["useruid"])) {
            echo "<a href='includes/logout.inc.php' class='nav-link'>Abmelden</a>";
        }
        else{
            echo "<a href='login.php' class='nav-link'>Anmelden</a>";
        }
    ?>
</button>

<?php
  include_once 'end-section.php';
?>

For the whole small project-code visit my Gitlab: https://gitlab.iet-gibb.ch/agn122703/secapp

Advertisement

Answer

you may put it on the top of your page. you never used or include functions.inc.php in your header.php file use:

<?php
session_start();
include_once 'functions.inc.php';
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement