Jump to content

$id variable is randomly set


Twinbird

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.