Jump to content

Why am I getting this error? Does not make sense.


Lamez

Recommended Posts

Hello, I am sick of getting this error. I will post it below, let me first explain how I get the error to appear. When I create a file in a sub-folder, and execute it, I get the error. However, I have a sub-folder with a script in it and it works just fine. I also tried to make a link to call a function, and I get the same error. Here is the error:

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/krazypickem/core/includes/functions.php on line 8

 

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /var/www/krazypickem/core/includes/functions.php on line 8

 

Warning: mysql_query() [function.mysql-query]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/krazypickem/core/includes/functions.php on line 167

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/krazypickem/core/includes/functions.php on line 167

 

Any ideas?

 

P.S. the line it is talking about is a query that has worked before, on a different page.

Link to comment
Share on other sites

I was trying to spear you guys, but since it is needed here is the index that is located in my "administration" directory:

<?php
$path = "../";
$title = "Member's Area";
$Login = true;
include ($path."core/main.php");

echo "Welcome ".getFullName();
$footer = true;
include ($path.$_core."main.php"); 
?>

 

here is the main.php

<?php
//ob_start(); //for debugging
if(!$footer){
include($path."core/includes/functions.php");
echo '<title>'.$title.'</title>';
}else{
echo '<br /><a href="'.$path.'index.php">Home</a> | <a href="'.$path.'register.php">Register!</a> | <a href="'.$path.'login.php">Login!</a>';
if(LoggedIn()){
	echo ' | <a href="'.$path.'user/">Member\'s Area</a> | <a href="?cmd=logout">Logout</a>';
}
if(isAdmin()){
	echo ' | <a href="'.$path.'administration/">Admin Area</a>';
}
if(isMod() || isAdmin()){
	echo ' | <a href="'.$path.'administration/moderators/">Mod Area</a>';
}
}
?>

 

Last but not least, the functions.php (the mac daddy)

<?php
include("db-config.php");
function clean($str){
$str = @trim($str);
if(get_magic_quotes_gpc()) {
	$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
function checkExist($col, $var){
$col = clean($col);
$var = clean($var);
$q = mysql_query("SELECT * FROM ".TBL_PEOPLE." WHERE $col = '$var' LIMIT 1")or die("Function Check Exist: ".mysql_error());
$n = mysql_num_rows($q);
if($n == 0){
	return false;
}else if($n > 0){
	return true;
}
}
function makeKey($id){
$key = $id.rand(0, 10); //To Be Unique 
for($j=0; $j<=3; $j++){
	$key .= rand(11, 20);
}
for($k=0; $k<=3; $k++){
	$key .= rand(21, 30);
}
return $key;
}
function RegisterUser($email, $email2, $first, $last, $pass, $pass2, $level){
resetArrays();
$email = strtolower(clean($email));
$email2 = strtolower(clean($email2));
$first = strtolower(clean($first));
$last = strtolower(clean($last));
$pass = clean($pass);
$pass2 = clean($pass2);
$level = clean($level);
$id = mysql_num_rows(mysql_query("SELECT * FROM ".TBL_PEOPLE));
addValue("email", $email);
addValue("email2", $email2);
addValue("first", $first);
addValue("last", $last);
addValue("pass", $pass);
addValue("pass2", $pass2);
addValue("level", $level);
if(!empty($email) && !empty($email2)){
	if(checkMatch($email, $email2) == true){
		if(checkEmail($email) == true){
			if(checkExist("email", $email) == false){
				$db_email = $email;
			}else{
				addError("email", "Email already exist.");
				addError("email2", "Email already exist.");
			}
		}else{
			addError("email", "Email is invalid.");
			addError("email2", "Email is invalid.");
		}
	}else{
		addError("email", "Emails do not match.");
		addError("email2", "Emails do not match.");
	}
}else{
	if(empty($email)){
		addError("email", "Email field is empty.");
	}
	if(empty($email2)){
		addError("email2", "Email field is empty.");
	}
}
if(!empty($pass) && !empty($pass2)){
	if(checkMatch($pass, $pass2) == true){
		if(checkPass($pass) == true && checkPass($pass2) == true){
			$db_pass = md5($id.$pass.$id);
		}else{
			if(checkPass($pass) == false){
				addError("pass", "Password is too small.");
			}
			if(checkPass($pass2) == false){
				addError("pass2", "Password is too small.");
			}
		}
	}else{
		addError("pass", "Passwords do not match.");
		addError("pass2", "Passwords do not match.");
	}
}else{
	if(empty($pass)){
		addError("pass", "Password field is empty.");
	}
	if(empty($pass2)){
		addError("pass2", "Password field is empty.");
	}
}
if(!empty($first)){
	$db_first = $first;
}else{
	addError("first", "First name is empty.");
}
if(!empty($last)){
	$db_last = $last;
}else{
	addError("last", "Last name is empty.");
}
if(checkForErrors($_SESSION['errorArray']) == true){
	return false;
}else{
	resetArrays();
	if($email == ADMIN){
		$level = 8;
	}else if($email == MOD){
		$level = 5;
	}
	$key = makeKey($id);
	mysql_query("
	INSERT INTO ".TBL_PEOPLE." (id, level, email, first, last, password, activated, act_key, online) 
	VALUES ('$id', '$level', '$db_email', '$db_first', '$db_last', '$db_pass', '0', '$key', '0')
	")or die(mysql_error());
	return true;
}
}
function LoginUser($email, $pass){
resetArrays();
$email = clean($email);
$pass = clean($pass);
addValue("email", $email);
$q = mysql_query("SELECT * FROM ".TBL_PEOPLE." WHERE email = '$email'");
$n = mysql_num_rows($q);
if($n == 0){
	addError("email", "Incorrect email or ");
	addError("pass", "incorrect password.");
}else{
	$f = mysql_fetch_array($q);
	$id = $f['id'];
	$level = $f['level'];
	$pass2 = $f['password'];
	$pass = md5($id.$pass.$id);
	if(checkMatch($pass, $pass2) == true){
		resetArrays();
		crunchCookie($id, $email);
		makeOnline($id);
		return true;
	}else{
		addError("email", "Incorrect email or ");
		addError("pass", "incorrect password.");
		return false;
	}
}
}
function crunchCookie($id, $email){
$cookie_host = preg_replace('|^www\.(.*)$|', '.\\1', $_SERVER['HTTP_HOST']);
setcookie("~JRL-email", $email, time()+3600, "/", $cookie_host);
setcookie("~JRL-id", $id, time()+3600, "/", $cookie_host);
}
function makeOnline($id){
$id = clean($id);
mysql_query("UPDATE ".TBL_PEOPLE." SET online = '1' WHERE id = '$id'");
}
function makeOffline($id){
$id = clean($id);
mysql_query("UPDATE ".TBL_PEOPLE." SET online = '0' WHERE id = '$id'");
}
function isOnline($email){
$q = mysql_query("SELECT * FROM ".TBL_PEOPLE." WHERE email = '$email'");
$f = mysql_fetch_array($q);
if($f['online'] == 1){
	return true;
}else{
	return false;
}
}
function logOut(){
makeOffline(getUserInfo("id"));
setcookie("~JRL-email", "", time()-3600, "/");
setcookie("~JRL-id", "", time()-3600, "/");
unset($_COOKIE['~JRL-email']);
unset($_COOKIE['~JRL-id']);

}
function LoggedIn(){
if(isset($_COOKIE['~JRL-email']) && isset($_COOKIE['~JRL-id'])){
	return true;
}else{
	return false;
}
}
function isAdmin(){
if(LoggedIn()){
	$level = getUserInfo("level");
	if($level == {
		return true;
	}else{
		return false;
	}
}else{
	return false;
}
}
function isMod(){
if(LoggedIn()){
	$level = getUserInfo("level");
	if($level == 5){
		return true;
	}else{
		return false;
	}
}else{
	return false;
}
}
function getUserInfo($var){
if(LoggedIn()){
$email = $_COOKIE['~JRL-email'];
$id = $_COOKIE['~JRL-id'];
	if($var == "email"){
		return $email;
	}else if($var == "id"){
		return $id;
	}else{
		$q = mysql_query("SELECT * FROM ".TBL_PEOPLE." WHERE id = '$id'") or die("FUNCTION 0 - getUserInfo ERROR: ".mysql_error());
		$f = mysql_fetch_array($q) or die("FUNCTION 1 - getUserInfo ERROR: ".mysql_error());
		return $f[$var];
	}
}else{
	return false;
}
}
function getFullName(){
$first = getUserInfo("first");
$last = getUserInfo("last");
return ucfirst($first)." ".ucfirst($last);
}
function checkEmail($email){
if (filter_var($email, FILTER_VALIDATE_EMAIL)){
    	return true;
}else{
    	return false;
}
}
function checkPass($pass){
$pass = strlen($pass);
$required = 6;
if($pass == $required){
	return true;
}else{
	return false;
}
}
function checkMatch($str1, $str2){
if($str1 === $str2){
	return true;
}else{
	return false;
}
}
function addError($field, $error){
$_SESSION['errorArray'][$field] = $error;
}
function addValue($field, $value){
$_SESSION['valueArray'][$field] = $value;
}
function getError($field){
return $_SESSION['errorArray'][$field];
}
function getValue($field){
return $_SESSION['valueArray'][$field];
}
function resetArrays(){
$_SESSION['errorArray'] = "";
$_SESSION['valueArray'] = "";
}
function checkForErrors($array){
if($array == ""){
	return false;
}else{
	return true;
}
}
function currentPage(){
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
?>

 

There is two more pages that are very important, but I don't think they have anything to do with my problem. Do you guys see any errors? I am lost.

Link to comment
Share on other sites

As requested:

db-config.php

<?php
include("wide-variables.php");
define('ADMIN', "admin@webby.com", true);
define('MOD', "mod@webby.com", true);

$DB_CONN = mysql_connect($_DB_SERVER, $_DB_USER, $_DB_PASS) or die('<font color=red><b>Site Returned Error:</b></font> '.mysql_error());
mysql_select_db($_DB_NAME, $DB_CONN) or die('<font color=red><b>Site Returned Error:</b></font> '.mysql_error());
?>

 

and wide-variables.php

<?php
//Start Session
session_start();
error_reporting(E_ALL);
//Page Variables
$_SESSION['currentPage'] = currentPage();

//Datbase Config
$_DB_USER = "x";
$_DB_PASS = "x";
$_DB_SERVER = "x";
$_DB_NAME = "x";
/*
$_DB_USER = "x";
$_DB_PASS = "x";
$_DB_SERVER = "x";
$_DB_NAME = "x";
*/
//Table Variables:
define('TBL_PEOPLE', "people", true);

//Path Variables:
$_pear = "";
$_core = "core/";
$_includes = "core/includes/";
$_style = "core/style/";
$_pages = "/pages/";

//Redirect Variables:
$_main_error = $path."index.php";
$_not_member_area = $path."index.php";

//If's
if(isset($Login) && $Login){
if(!LoggedIn()){
	header("Location: ".$_not_member_area);
}	
}
if(isset($AdminOnly) && $AdminOnly){
if(!LoggedIn()){
	header("Location: ".$_not_member_area);
}else if(!isAdmin()){
		header("Location: ".$_not_member_area);
}
}
if(isset($ModOnly) && $ModOnly){
if(!LoggedIn()){
	header("Location: ".$_not_member_area);
}else if(!isAdmin() || isMod()){
	header("Location: ".$_not_member_area);
}
}
//CMD Stuff
if(isset($_GET['cmd'])){
$cmd = $_GET['cmd'];

//Logout
if($cmd == "logout"){
	logOut();
	header("Location: ".$_not_member_area);
}
}
?>

Link to comment
Share on other sites

If you were to put the following two lines of code into the main file being requested, immediately after the first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(E_ALL);

you would probably find out which include statement is failing due to the path being incorrect or perhaps due to a conditional statement being FALSE (with the include inside of the conditional.)

 

You need to get your include statements under control. You should not have them 4-5 levels deep. You should have a main application file that includes things it needs, period, i.e. one level of include files.

 

You should also only put any error_reporting/display_errors settings into your main file, so that you can take them out later and so that they are in effect for all the remainder of the code on the page. You should in fact set those two settings globally in your master php.ini (or a .htaccess file or a local php.ini.) When you see a suggestion in a forum like this to add those two settings in a script, it is for the case where "my code does not work and it is not telling me why" and we need to get some unconditional help from php's error_reporting/display_errors. In your case, where the error_reporting setting is in wide-variables.php, it is probably not doing any good because that code is never being included.

Link to comment
Share on other sites

I will move the error_reporting line. Yes the wide-variables.php is being included, because I got errors before, and fixed them immediately. I don't think the includes are "out of control", and I will keep them the same. All the files seem to be included, for some very odd reason, I cannot connect to the database from that folder. I can from the members folder. I still have no clue why.

Link to comment
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.