简体   繁体   中英

how does $_session work? and have i done something wrong? php

i am new to php and coding in general. i wanted to make a signup/login system. the signup and login function works but when i login i want to stay on index.php and change the context. but for some reason it doesn't work. have i done something wrong? this is my login code: (gebruikersnaam = username & wachtwoord = password)

    <?php
if(!isset($_SESSION)){
    session_start();
}

include "../../db/dbconnect.php";
include "logs.inc.php";


$gebruikersnaam = mysqli_real_escape_string( $db, $_POST['gebruikersnaam'] );
$wachtwoord = mysqli_real_escape_string( $db, $_POST['wachtwoord'] );
$salt = "46elg9hl50h[[erlt".$wachtwoord."tesy45745jytj57sasfq";

if( empty( $gebruikersnaam ) || empty( $wachtwoord ) ){
    header( "Location: ../index.php?/Fields=Empty" );
    exit();
}

$sql = mysqli_query( $db, "SELECT * FROM organisatie WHERE OrgGebNaam='$gebruikersnaam' OR OrgEmail='$gebruikersnaam'" );
$result = mysqli_num_rows( $sql );

if( $result < 1 || $result > 1){
    header( "Location: ../index.php?/Error/result" );
    exit();
}
else {
    if( $row = mysqli_fetch_assoc( $sql ) ){
        $hashCheck = password_verify( $salt, $row['OrgWW']);
        if( $hashCheck == false ){
            header( "Location: ../index.php?/Error/Password_Incorrect#1");
            exit();
        }
        else if( $hashCheck == true ){
            $_SESSION['ID'] = $row['gebruikerID'];
            $_SESSION['Geb'] = $row['gebruikersnaam'];
            log_ingelogd();
            header( "Location: ../index.ad.php?/Welcome.".$gebruikersnaam );
            exit();
        }
    }
    else{
        header( "Location: ../index.php?Error/Password_Incorrect#2");
        exit();
    }
}
?>

and this is index.php:

    <?php
    include 'header.php';
    include '../db/dbconnect.php';
    error_reporting(0);

    if( !isset( $_SESSION['ID']) && !isset( $_SESSION['Geb'])) {
    ?>
        <section>
            <table id="login-table">
                    <form action="include/login2.inc.php" method="POST">
                    <tr>
                        <td colspan=2 style="text-align: center;"> Login Formulier</td>
                    </tr>
                    <tr>
                        <td><div class="table-text">Gebruikersnaam: </div></td>
                        <td><input type="text" class="login-table-text" name="gebruikersnaam" required title="Vul hier uw gebruikersnaam in." placeholder="Gebruikersnaam"></td>
                    </tr>
                    <tr>
                        <td><div class="table-text">Wachtwoord: </div></td>
                        <td><input type="password" class="login-table-text" name="wachtwoord" required title="Vul hier uw wachtwoord in." placeholder="Wachtwoord"></td>
                    </tr>
                    <tr>
                        <td colspan=2 style="text-align: center;"><button id="login-btn" name="submit">Inloggen</button></td>
                    </tr>
                </form>
                <form action="signup.php" method="POST">
                    <tr>
                        <td colspan=2><hr></td>
                    </tr>
                    <tr>
                        <td><div class="table-text">Als u geen account heeft: </div></td>
                    </tr>
                    <tr>
                        <td colspan=2 style="text-align: center;"><button id="signup-btn" name="signup">Maak een account aan ></button></td>
                    </tr>
                </form>
            </table>
        </section>
    <?php
    } // ending if statement.
    $gebruikersnaam = $_SESSION['Geb'];
    $sql = mysqli_query( $db, " SELECT * FROM gebruikers WHERE gebruikersnaam = '$gebruikersnaam'" );
    $row = mysqli_fetch_assoc( $sql );
    if( $row['IsAdmin'] == 1){  
    ?>
    <div class="container">
        <div class="box">
            <table class="box-information">
                <tr class="box-information-titel">
                    <td>Gebruikers</td>
                </tr>
            </table>
            <p class="box-information-omschrijving">Bekijk hier de gegevens van alle gebruikers, maak gebruikers aan, verwijder gebruikers en bewerk de gegevens van gebruikers.</p>
            <div id="box-btn-gebruiker">
                <a href="gebruikers.php"><button class="box-btn">Bekijk ></button></a>
            </div>
        </div>
        <div class="box">
            <table class="box-information">
                <tr class="box-information-titel">
                    <td>Onderhoud</td>
                </tr>
            </table>
            <p class="box-information-omschrijving">Bekijk hier het onderhouds tabel zodat u onderhoud kan uitvoeren.</p>
            <div id="box-btn-onderhoud">
                <a href="onderhoud.php"><button class="box-btn">Bekijk ></button></a>
            </div>
        </div>
        <div class="box">
            <table class="box-information">
                <tr class="box-information-titel">
                    <td>Organisatie</td>
                </tr>
            </table>
            <p class="box-information-omschrijving">Bekijk hier alle gegevens die een organisatie heeft.</p>
            <div id="box-btn-organisatie">
                <button class="box-btn">Bekijk ></button>
            </div>
        </div>
        <div class="box">
            <table class="box-information">
                <tr class="box-information-titel">
                    <td>Factuur</td>
                </tr>
            </table>
            <p class="box-information-omschrijving">Bekijk hier alle openstaande facturen, maak facturen en factuurregels.</p>
            <div id="box-btn-factuur">
                <button class="box-btn">Bekijk ></button>
            </div>
        </div>
    </div>
    <?php
    }

    include 'footer.php';
?>

can someone tell me what i did wrong? edit: (im not sure but...) i think the problem lies here:

$gebruikersnaam = $_SESSION['Geb'];
$sql = mysqli_query( $db, " SELECT * FROM gebruikers WHERE gebruikersnaam = '$gebruikersnaam'" );
$row = mysqli_fetch_assoc( $sql );
if( $row['IsAdmin'] == 1){

edit: this is my header.php:

<?php
if(!isset($_SESSION)){
    session_start();
}

?>

Maybe missing session_start(); in file index.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM