Jump to content

$id variable is randomly set


Go to solution Solved by Ch0cu3r,

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.

Edited by Twinbird
Link to comment
https://forums.phpfreaks.com/topic/284526-id-variable-is-randomly-set/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.