Jump to content

Recommended Posts

Hello all!

 

Im using php5.2.1 w/ mysql 5.0

 

Im having some trouble with my on of my class functions. On processing a logon, the following error occurs:

 

Fatal error: Call to undefined function generateRandID() in C:\webserver\htdocs\papple\include\session.php on line 97

 

line 97 reads:

$this->userid = $_SESSION['userid'] = $this->generateRandID();

 

generating a uid for my cookies.

 

the function that is called is:

 

function generateRandID(){

return md5($this->generateRandStr(16));

}

 

function generateRandStr($length){

$randstr = "";

for($i=0; $i<$length; $i++){

$randnum = mt_rand(0,61);

if($randnum < 10){

$randstr .= chr($randnum+48);

}else if($randnum < 36){

$randstr .= chr($randnum+55);

}else{

$randstr .= chr($randnum+61);

}

}

return $randstr;

}

}

 

Any help would be great,

Thanks in anticipation,

Moron!

$this->userid = $_SESSION['userid'] = $this->generateRandID();

 

I may not know what I am talking about since I do not have any other code to look at, but if you are trying to cal a function of a class outside the class don't you need to instantiate it? IE:

 

<?php
$genClass = new genereateClass();
$userid = $_SESSION['userid'] = $genClass->generateRandID();
?>

 

???

What I am grasping at is more code would be very helpful.

Hello Frosty, the functions are all within the same class. the session class is listed below. the generate rand ID functions ar e close to the bottom of the code.

 

Thanks,

 

<?php #session class object

include("database.php");
include("mailer.php");
include("form.php");

class Session
{
var $username;
var $userid;
var $userlevel;
var $logged_in;
var $time;
var $userinfo = array();
var $url;
var $referrer;

function Session(){
	$this->time = time();
	$this->startSession();
}
function startSession(){
	global $database;
	session_start();

	$this->logged_in = $this->checkLogin();


	if(!$this->logged_in){
		$this->username = $_SESSION['username'] = GUEST_NAME;
		$this->userlevel = GUEST_LEVEL;
	}

	if(isset($_SESSION['url'])){
		$this->referrer = $_SESSION['url'];
	}else{
		$this->referrer = "/";
	}

	$this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
}

function checkLogin(){
	global $database;
	if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
		$this->username = $_SESSION['username'] = $_COOKIE['cookname'];
		$this->userid = $_SESSION['userid'] = $_COOKIE['cookid'];
	}
	if(isset($_SESSION['username']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME){
		if($database->verifyUser($_SESSION['username'], $_SESSION['userid']) != 0){
			unset ($_SESSION['username']);
			unset ($_SESSION['userid']);
			return false;
		}
		$this->userinfo  = $database->getUserInfo($_SESSION['username']);
		$this->username  = $this->userinfo['username'];
		$this->userid    = $this->userinfo['userid'];
		$this->userlevel = $this->userinfo['userlevel'];
		return true;
	}else{
		return false;
	}
}

function login($subuser, $subpass){
	global $database, $form;
	$field = "user";
	if(!$subuser || strlen($subuser = trim($subuser)) == 0){
		$form->setError($field, "* Username not Entered");
	} else {
		if(!eregi("^([0-9a-z])*$", $subuser)){
			$form->setError($field, "* Username not alphanumeric");
		}
	}
	if(!$subpass){
		$form->setError($field, "* Password not entered");
	}
	if($form->num_errors > 0){
		return false;
	}
	$subuser = stripslashes($subuser);
	$result = $database->verifyUser($subuser, md5($subpass));

	if($result == 1){
		$field = "user";
		$form->setError($field, "please re-enter your username");
	}
	else if($result == 2){
		$field = "pass";
		$form->setError($field, "invalid password");
	}
	if ($form->num_errors > 0){
		return false;
	}
	$this->userinfo  = $database->getUserInfo($subuser);
	$this->username  = $_SESSION['username'] = $this->userinfo['username'];
	$this->userid    = $_SESSION['userid'] = generateRandID();
	$this->userlevel = $this->userinfo['userlevel'];

	$database->updateUserInfo($this->username, "userid", $this->userid);

	#set cookie time by default.

	setcookie("cookname", $this->username, time()+COOKIE_EXPIRE, COOKIE_PATH);
	setcookie("cookid",   $this->userid,   time()+COOKIE_EXPIRE, COOKIE_PATH);

	#login complete successfully

	return true;
}

#user logout method.

function logout(){
	global $database;

	if(isset($_COOKIE['cookname']) && isset($_COOKIE['cookid'])){
		setcookie("cookname", "", time()-COOKIE_EXPIRE, COOKIE_PATH);
		setcookie("cookid",   "", time()-COOKIE_EXPIRE, COOKIE_PATH);
	}
	unset($_SESSION['username']);
	unset($_SESSION['userid']);

	$this->logged_in = false;

	$this->username  = GUEST_NAME;
      	$this->userlevel = GUEST_LEVEL;
}

#Enter user details 	
function register($subuser, $subpass, $subemail){
	global $database, $form, $mailer;

	$field = "user";
	if(!$subuser || strlen($subuser = trim($subuser)) == 0){
		$form->setError($field, "* Please enter a usernam");

	} else {

		if(strlen($subuser) > 3){
            $form->setError($field, "* Username length over 3 characters");

		} else if(!eregi("^([0-9a-z])+$", $subuser)){
			$form->setError($field, "* Username not alphamerical");

		} else if(strcasecmp($subuser, GUEST_NAME) == 0){
			$form->setError($field, "* Username reserved");

		} else if($database->userExists($subuser)){
			$form->setError($field, "* Username specified to another user");

		}
	}

	#verify the given password! d-_-b

	$field = "pass";
	if(!$subpass){
		$form->setError($field, "* No password entered");
	} else {
		$subpass = stripslashes($subpass);
		if(strlen($subpass) < 6){
			$form->setError($field, "* Password must be more than 6 characters");
		} else if(!eregi("^([0-9a-z])+$", ($subpass = trim($subpass)))){
			$form->setError($field, "* Password is not alphanumeric");
		}
	}

	#this section makes sure that the email address entered is a valid email address.

	$field = "email";
	if(!$subemail || strlen($subemail = trim($subemail)) == 0){
		$form->setError($field, "* Email not entered");
	} else {
		$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
				 ."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
                	 ."\.([a-z]{2,}){1}$";
		if(!eregi($regex,$subemail)){
			$form->setError($field, " *Email Invalid");
		}
		$subemail = stripslashes($subemail);
	}

	#If an error exists force user to ammend.

	if($form->num_errors > 0){
		return 1;
	} else {
		if($database->addNewUser($subuser, md5($subpass), $subemail)){
			if(EMAIL_WELCOME){
				$mailer->sendWelcome($subuser, $subemail, $subpass);
			}
			return 0;
		}else{	
			return 2;
		}
	}
}			

function editAccount($subcurpass, $subnewpass, $subemail){
	global $database, $form;

	if($subnewpass){
		$field = "curpass";
		if(!$subnewpass){
			$form->setError($field, "* Current Password not entered");
		} else {
			$subcurpass = stripslashes($subcurpass);
			if($strlen($subcurpass) < 6 || !eregi("^([0-9a-z])+$", ($subcurpass = trim($subcurpass)))){
				$form->setError($field, "* Invalid Current Password");
			} 
			if($database->verifyUser($this->username,md5($subcurpass)) != 0){
				$form->setError($field, "* Invalid Current Password");
			}
		}
		$field = "newpass";
		$subpass = stripslashes($subnewpass);
		if(strlen($subnewpass) < 6){
			$form->setError($field, "* New Password too short - 6 or more characters");

		} else if(!eregi("^([0-9a-z])+$", ($subnewpass = trim($subnewpass)))){
            $form->setError($field, "* New Password not alphanumeric");
         	
		} else if($subcurpass){
			$field = "newpass";
			$form->setError($field, "* New password not entered");
		}
		$field = "email";  
		if($subemail && strlen($subemail = trim($subemail)) > 0){
			$regex = "^[_+a-z0-9-]+(\.[_+a-z0-9-]+)*"
					."@[a-z0-9-]+(\.[a-z0-9-]{1,})*"
					."\.([a-z]{2,}){1}$";
			if(!eregi($regex,$subemail)){
				$form->setError($field, "* Email invalid");
			}
			$subemail = stripslashes($subemail);
		}
		if($form->num_errors > 0){
			return false; 
		}
		if($subcurpass && $subnewpass){
			$database->updateUserInfo($this->username,"password",md5($subnewpass));
		}
		if($subemail){
			$database->updateUserInfo($this->username,"email",$subemail);
		}
		return true;
   }
function isAdmin(){
	return ($this->userlevel == ADMIN_LEVEL ||
	$this->username  == ADMIN_NAME);
}
function generateRandID(){
	return md5($this->generateRandStr(16));
}

function generateRandStr($length){
	$randstr = "";
	for($i=0; $i<$length; $i++){
		$randnum = mt_rand(0,61);
		if($randnum < 10){
			$randstr .= chr($randnum+48);
		}else if($randnum < 36){
			$randstr .= chr($randnum+55);
		}else{
			$randstr .= chr($randnum+61);
		}
	}
	return $randstr;
	}
}

};

$session = new Session;
$form = new Form;

?>

your class differs to your example.

 

In Session::login, this:

 

$this->userid    = $_SESSION['userid'] = generateRandID();

 

should be as it is in your first post:

 

$this->userid    = $_SESSION['userid'] = $this->generateRandID();

my bad. it actually reads $this->userid    = $_SESSION['userid'] = $this->generateRandID(); now...sthe error has slightly changed to :

 

Fatal error: Call to undefined method Session::generateRandID() in C:\webserver\htdocs\papple\include\session.php on line 97

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.