-2

[18-Dec-2014 01:18:54 Europe/Helsinki] PHP Notice: Undefined index: username in /home/stance93/public_html/game/core.php on line 738

on this line i have

if (@!isset($_SESSION['username'])) {
    echo '<script>window.location = "index"</script>';
}

$username = $_SESSION['username'];

[18-Dec-2014 01:18:54 Europe/Helsinki] PHP Notice: Undefined index: username in /home/stance93/public_html/game/core.php on line 946 on this line i have

<?php
    $username = $_SESSION['username'];
    $uid      = get_userid($username);
?>

and another one on line 995 with :

$user       = $_SESSION['username'];
$selectuser = mysql_query("SELECT * FROM users WHERE username='$user'");
$urow       = mysql_fetch_array($selectuser);

my error log is more than 150Mb with this errors , and with "$urow" that calls the username , what is wrong ? the functions are running well , its showing me the avatar of the user for ex : $urow['picture']" , but in the error log it's telling me : PHP Notice: Undefined variable: urow in another page . All my page's are includeing core.php , i have not redeclared $urow , because it is declared in core , i repeat , on every page not showing errors , only in the errorlog..scuze me for my bad english :)

rpV
  • 17
  • 6

1 Answers1

2

The most likely issue causing this is that you don't have session_start() at the beginning of the script. Meaning you won't be able to access any $_SESSION variables.

Change your code to this:

session_start();
if (!isset($_SESSION['username'])) {
    die(header("Location: index"));
}

$username = $_SESSION['username'];

Notes

You should never suppress (@) any calls within your code. Always fix the issue, don't ignore it.

Your script was still going to run before redirecting it since you never explicitly end the script when the $_SESSION value isn't set. (see the die(header("Location: index")) in the code above.)

You should probably also check to make sure it isn't empty:

if(!isset($_SESSION['username']) || empty($_SESSION['username'])) {
    die(header("Location: index"));
}
Darren
  • 13,050
  • 4
  • 41
  • 79
  • on line 2 in core : ob_start(); session_start(); – rpV Dec 18 '14 at 01:44
  • Then your issue is as stated above, you don't kill the script when the username isn't found. The above code will work :) – Darren Dec 18 '14 at 01:45
  • thank you for reply me , the core , is made in 2 parts , first is wen the user is logged in , and another wen the user is not logged , on line 10, before of the 2 parts, before of any funtions declared i have this : if (@!$_SESSION['username']) { echo ' '; exit(); } , and declared again on the part were the user is logged in , just in case if matters to know better my problem – rpV Dec 18 '14 at 01:50
  • i changed the line 11 the above code , with your code , and its seems to working fine ,thank you – rpV Dec 18 '14 at 01:58