1

EDIT: is there a way to do this without reloading the page? I have a class that shows up when "Login" is pressed but it goes away quickly because the page is reloaded

I'm trying to test to see if a specific link text has been clicked because the same link can either have the texts "Login" or "Logout", but it's not registering that it's a valid link at all

index.php:

 <body>
        <nav>
            <ul>
                <li id="login">
                    <?php
                        if (isset($_SESSION['logged_user'])) 
                            echo "<a href='index.php?link=login'>Login";
                        else
                            echo "<a href='index.php?link=logout'>Logout";
                    ?>
                    </a>
                </li>

            </ul>
        </nav>

//LOGOUT
                if ($_GET['link']=='logout') {
                    unset($_SESSION["logged_user"] );
                    unset( $_SESSION );
                    $_SESSION = array();
                    session_destroy();
                }

error: Undefined index: link

stumped
  • 3,235
  • 7
  • 43
  • 76
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – user3942918 Mar 28 '17 at 21:54

3 Answers3

1

Change this to

if ($_GET['link']=='logout') {

this

if (isset($_GET['link']) && $_GET['link']=='logout') {
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

When PHP page gets load, the PHP compiler checks for link property in GET array which is not there until you clicked on it. So you have to take care this condition with isset function so that it only validates when its available.

 if (isset($_GET['link']) && $_GET['link']=='logout')

Hope this helps!!

Pramod Patil
  • 2,704
  • 2
  • 14
  • 20
1

I guess session_start(); is probably missing at the top to allow you to use $_SESSION['logged_user'];

  if(session_status() == PHP_SESSION_NONE){
        session_start();
    }

And also :

<?php
     if (isset($_GET['link']) && $_GET['link']=='logout') {
          unset($_SESSION["logged_user"] );
          unset( $_SESSION );
          $_SESSION = array();
          session_destroy();
         //  echo "Hello";
     }
    ?>

Good luck

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24