joebegborg07
joebegborg07

Reputation: 839

Cannot understand why undefined index

I'm still new to php and still experimenting about. I'm getting an undefined index for the variable :

$httpreferer = $_SERVER['HTTP_REFERER'];

The entire code of the page is:

<?php

ob_start();
session_start();
$current_file = $_SERVER['SCRIPT_NAME'];
$httpreferer = $_SERVER['HTTP_REFERER'];

function loggedin(){
    if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])){
        return true;
    }
    else
    {
        return false;
    }
}

?>

Sorry if this is a lame question. I'm still a beginner.

Thanks in advance.

Upvotes: 0

Views: 111

Answers (2)

Silviu-Marian
Silviu-Marian

Reputation: 10907

If the visitor doesn't have a referrer (for various reasons he may not, like directly accessing a page - and not clicking a link on another website, or coming from an HTTPS link), then this variable will not be there.

What you can do is this

$httpreferer = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];

This is a ternary operator. http://php.net/manual/en/language.operators.comparison.php

Upvotes: 0

ComFreek
ComFreek

Reputation: 29424

The browsers (clients) are free to send any HTTP headers they like. You cannot trust them!

Check whether the client provided one using isset():

if (isset($_SERVER['HTTP_REFERER'])) {
  // do something
}

Bear in mind that this not tell anything about the data itself. It may be anything.

Upvotes: 2

Related Questions