Hi guys,

I am still kind of struggeling with my upgrade from php4 to php5, my site is almost there but I can't figure out how to set my http_referer.

This is how it looks like today:

<?php
 $_SESSION["referer"] .= $_SERVER['HTTP_REFERER'];
 $my_web = $_SESSION["referer"]
?>

The error message I get is "undefined variable: _SESSION and undefined index: referer".

I have tried to put isset() everywhere, but I can not figure out the soulution.

Any suggestions? :)

Dani AI

Generated

Brief summary: the "undefined variable: _SESSION" and "undefined index: referer" errors came from using the session array before a session was started and from assuming HTTP_REFERER always exists. As experienced, missing initialization and a small syntax mistake in an if caused the parse error; 's later posts show the right direction (start the session, guard the variables).

Key fixes and modern best practices:

  • session_start() must run before any output (HTML/echo) so the $_SESSION superglobal is available.
  • Never concatenate onto a session key that might not exist. Either initialize it (or use the null-coalescing approach on modern PHP: $_SESSION['referer'] = ($_SESSION['referer'] ?? '') . ($_SERVER['HTTP_REFERER'] ?? '');) or keep referers as an array ($_SESSION['referers'][]) for easier handling.
  • Always check that $_SERVER['HTTP_REFERER'] is set before using it and validate it (for example with filter_var(..., FILTER_VALIDATE_URL)) — HTTP_REFERER is optional and user-controlled.

Reliability and security notes:

  • HTTP_REFERER is unreliable (may be stripped by browsers, proxies, or extensions) and can be spoofed; it should never be used for authentication or authorization decisions. For reliable navigation tracking, use explicit parameters, hidden form fields, or server-side routing logic instead of relying on the referer header.
  • Prefer storing structured data in session (arrays, timestamps) rather than building a single concatenated string.

Quick debugging tips:

  • For parse errors, look for mismatched parentheses/braces (that was the cause of the "unexpected '{'").
  • Enable development errors with error_reporting(E_ALL); ini_set('display_errors', 1); and inspect var_dump($_SESSION) or server logs to confirm values.
  • After the above fixes (as shown in the thread by ), the approach used by is robust for simple, non-security-critical referer tracking.

Recommended Answers

All 6 Replies

Your script should be the following:

<?php session_start();
 $_SESSION["referer"] .= $_SERVER['HTTP_REFERER'];
 $my_web = $_SESSION["referer"]
?>

Also you will need to find another alternative to $_SERVER as it isn't always that accurate. The reason being is users can disable that information being sent.

Thanks for the suggestion, but it still doesn't work. I still get the same undefined variable/index errors :(

Ow now I see. Another bug. Try this.

<?php session_start();
if (!isset($_SESSION['referer']) { 
    $_SESSION['referer']='';
    }
$_SESSION['referer'].=$_SERVER['HTTP_REFERER']."\n";
$my_web = $_SESSION['referer']
?>

I think it's something along these lines i need. But now i got an error saying "unexpexted '{' in line 2". When i take the '{' away, obviously I get an other error. I also tried to mix up the code a little, but i still can't get it right.

OMG these bugs keep on coming. Did you hide any more for me to fix?

<?php session_start();
if (!isset($_SESSION['referer'])) { 
    $_SESSION['referer']='';
    }
$_SESSION['referer'].=(isset($_SERVER['HTTP_REFERER']))?($_SERVER['HTTP_REFERER']."\n"):'';
$my_web = $_SESSION['referer'];
?>

This time I tested it :)

Thank you so much! Now it work's perfectly :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.