Sessions in PHP

Discussion in 'PHP' started by ViggoAvatar, Feb 23, 2016.

  1. #1
    Hello there,
    I have been busy building a login system lately, and now everything but 1 thing works.
    So when you are logged in you should be redirected to home.php, and when you are not logged in you should be redirected to index.php, so i came with this bit of code:
    <?php
    session_start();
    if($_SESSION["login_user"] = true)
    {
    ?>
    
    //html goes in here
    
    <?php
    }
    else {
    header("location: /index.php");
    }
    ?>
    PHP:
    but i wouldnt have come here if this worked. Here is the problem: it throws the system into an infinite loop, until chrome terminates it... so i made a logout.php to end any session that may be there (at least i think)
    <?php
    session_start();
    session_destroy();
    ?>
    PHP:
    Doesnt fix anything...

    So, could somebody check these 2 items out, and tell me what i am doing wrong?
    (if anybody needs it:
    not logged in homepage: /index.php
    logged in homepage: /home.php
    logout page: /logout.php
    login code: /login.php

    thankyou in advance!
     
    ViggoAvatar, Feb 23, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're not checking the $_SESSION['login_user'], you're assigning it.

    Change the first line to:
    
    if ($_SESSION['login_user'] == true) {
    
    Code (markup):
     
    PoPSiCLe, Feb 24, 2016 IP
  3. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #3
    On top:

    if ( !isset($_SESSION["login_user"] ) {
    header("location: /index.php");
    }
    ...........
    then here is the script

    more elegant! :))
     
    Blizzardofozz, Mar 10, 2016 IP