Twinbird Posted December 4, 2013 Share Posted December 4, 2013 Hello, Nowhere in my code do I declare/create an $id variable. However, when I echo $id, I get a number! index.php: <?php require_once 'access.php'; userIsLoggedIn(); echo $id; // This outputs a number, yet I do not define or use $id anywhere! exit(); access.php: <?php function userIsLoggedIn() { if(!empty($_SERVER['REMOTE_USER'])) { $uid = $_SERVER['REMOTE_USER']; } if (isset($uid) and databaseContainsUserid($uid)) { return TRUE; } else { return FALSE; } } function userIsAdmin() { $uid = $_SERVER['REMOTE_USER']; if (isset($uid) and databaseContainsUseridAdmin($uid)) { return TRUE; } else { return FALSE; } } function databaseContainsUserid($userid) { include 'db.php'; try { $sql = 'SELECT id, name FROM user WHERE userid = :userid and type > 0'; $s = $pdo->prepare($sql); $s->bindValue(':userid', $userid); $s->execute(); } catch (PDOException $e) { echo 'Error searching for userid. ' . $e; exit(); } $row = $s->fetch(); if ($row['id'] > 0) { $GLOBALS['id'] = $row['id']; $GLOBALS['name'] = $row['name']; return TRUE; } else { return FALSE; } } function databaseContainsUseridAdmin($userid) { include 'db.php'; try { $sql = 'SELECT COUNT(*) FROM user WHERE userid = :userid AND type = 2'; $s = $pdo->prepare($sql); $s->bindValue(':userid', $userid); $s->execute(); } catch (PDOException $e) { echo 'Error searching for userid. ' . $e; exit(); } $row = $s->fetch(); if ($row[0] > 0) { return TRUE; } else { return FALSE; } } db.php: <?php try { $pdo = new PDO('mysql:host=localhost;dbname=****', '****', '****'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $pdo->exec('SET NAMES "utf8"'); } catch (PDOException $e) { echo 'Unable to connect to database server. ' . $e; exit(); } Using echo statements, I can only determine that $id is set (to the number 5, in my case) after I call the function userIsLoggedIn. At no other point before this (including inside the userIsLoggedIn function in access.php) is $id defined. This makes no sense to me. Can anyone shine some light on the issue? Thanks. Link to comment https://forums.phpfreaks.com/topic/284526-id-variable-is-randomly-set/ Share on other sites More sharing options...
Ch0cu3r Posted December 4, 2013 Share Posted December 4, 2013 You are setting $id as a global variable within the databaseContainsUserid function. $GLOBALS['id'] = $row['id']; Link to comment https://forums.phpfreaks.com/topic/284526-id-variable-is-randomly-set/#findComment-1461259 Share on other sites More sharing options...
JonnoTheDev Posted December 4, 2013 Share Posted December 4, 2013 Avoid global variables. You have just discovered the pitfall of using them! Link to comment https://forums.phpfreaks.com/topic/284526-id-variable-is-randomly-set/#findComment-1461260 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.