Jump to content

Not accessing class attributes and methods as I expected for Logging system


sfc

Recommended Posts

I am very much in the learning process of PHP and programming in general.  I have a feeling if I get this concept I will be well on my way so your help is greatly appreciated.

 

I want to write a line to my "log.txt" file whenever someone logs in. Can you help me figure out what I am doing wrong?  This is all experimental and is not heading for production, at least not for a long time.

 

Here is the error I am getting:

 

 

Notice: Undefined variable: user_id in C:\wamp\www\photo_gallery\includes\session.php on line 25

 

Fatal error: Cannot access empty property in C:\wamp\www\photo_gallery\includes\session.php on line 25

 

Here is the login.php file:

<?php
require_once("../../includes/initialize.php");   

if($session->is_logged_in()) {
redirect_to("index.php");
}

//Check to see if page was submitted
if(isset($_POST['submit'])) {

$username = trim($_POST['username']);
$password = trim($_POST['password']);

//Check database to see if username/password exist.

  $found_user = User::authenticate($username, $password);
  
  if($found_user) {
  $session->login($found_user);
  //header("Location: index.php");
  }  else {
  $message = "Username/password combination incorrect.";
  }


} else {

$username = "";
$password = "";
}
?> 
<html>
<head>
<link href="../stylesheets/main.css" media="all" rel="stylesheet" type="text/css" />
<title>Photo Gallery</title>
</head>
<body>
<div id="header">
<h1>Photo Gallery</h1>
</div>
<div id="main">
<h2>Staff Login</h2>
<?php echo output_message($message)?>

<form action="login.php" method="post">
<table>
	<tr>
		<td>Username:</td>
		<td>
			<input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" />
		</td>
	</tr>
	<tr>
		<td>Password:</td>
		<td>
			<input type="password" name="password" maxlength="30" value="<?php htmlentities($password); ?>" />
		</td>
	</tr>
	<tr>
	<td colspan="2">
		<input type="submit" name="submit" value="Login" />
	</td>
	</tr>
</table>
</form>
</div>
<div id="footer">
Copyright <?php echo date("Y", time()); ?>
</div>
</body>
</html>

<?php if(isset($database)) { $database->close_connection();} ?>

 

Here is the initialize.php:

<?php

defined('SITE_ROOT') ? null : define('SITE_ROOT', "/" . 'wamp' . "/". 'www' . "/" . 'photo_gallery' );

defined('LIB_PATH') ? null : define ('LIB_PATH', SITE_ROOT . "/" . 'includes');

require_once(LIB_PATH."/"."config.php"); 
require_once(LIB_PATH."/"."functions.php");  
require_once(LIB_PATH."/"."session.php"); 
require_once(LIB_PATH."/"."database.php");
require_once(LIB_PATH."/"."database_object.php");
require_once(LIB_PATH."/"."user.php");
require_once(LIB_PATH."/"."logger.php");
?>

 

Here is the user.php:

<?php
// If it's going to need the database, then it's 
// probably smart to require it before we start.
require_once(LIB_PATH."/".'database.php');

class User extends DatabaseObject {

	public $id;
	public $username;
	public $password;
	public $first_name;
	public $last_name;



	public function full_name() {
		if(isset($this->first_name) && isset($this->last_name)) {
			return $this->first_name . " " . $this->last_name;
		} else {
			return "";
		}
	}

	public static function authenticate($username="", $password="") {
		global $database;
		$username = $database->escape_value($username);
		$password = $database->escape_value($password);

		$sql  = "SELECT * FROM users ";
		$sql .= "WHERE username = '{$username}' ";
		$sql .= "AND password = '{$password}' ";
		$sql .= "LIMIT 1";
		$result_array = self::find_by_sql($sql);
		return !empty($result_array) ? array_shift($result_array) : false;
	}


}

?>

 

Here is the session.php:

<?php
require_once("logger.php");
// A class to help work with Sessions
// In our case, primarily to manage logging users in and out

// Keep in mind when working with sessions that it is generally 
// inadvisable to store DB-related objects in sessions

class Session {

private $logged_in=false;
public $user_id;

function __construct() {
	session_start();
	$this->check_login();
if($this->logged_in) {
  // actions to take right away if user is logged in
} else {
  // actions to take right away if user is not logged in
}
}

public function return_userid() {
	  return $this->$user_id;
}
  public function is_logged_in() {
return $this->logged_in;
  }

public function login($user) {
// database should find user based on username/password
if($user){
  $this->user_id = $_SESSION['user_id'] = $user->id;
  $this->logged_in = true;
  logger::log_action("logged in");
}
  }
  
  public function logout() {
unset($_SESSION['user_id']);
unset($this->user_id);
$this->logged_in = false;
  }

private function check_login() {
if(isset($_SESSION['user_id'])) {
  $this->user_id = $_SESSION['user_id'];
  $this->logged_in = true;
} else {
  unset($this->user_id);
  $this->logged_in = false;
}
  }
  
}

$session = new Session();

?>

 

Here is the logger.php:

<?php (LIB_PATH."/".'session.php'); 
  
  class logger {
  
  private static $file =  "/wamp/www/photo_gallery/logs/log.txt";
  
  static function log_action($action="", $message = "") {
			global $session;
		  $handle = fopen(logger::$file, 'at');
		  fwrite($handle, date("Y-m-d H:i:s"). "| Login: " . $session->return_userid() . " " . $action. "\n");
		  fclose($handle);	
	  }
	  
  }
  
  
?>

Link to comment
Share on other sites

Thanks...sorry for the stupid miss.

 

I do have another question:  Why is this not working in my logger.php file?

 private static $file = SITE_ROOT . "/logs/log.txt"; 

 

In the initialize.php file I define SITE_ROOT as:

defined('SITE_ROOT') ? null : define("SITE_ROOT", "/" . 'wamp' . "/". 'www' . "/" . 'photo_gallery' );

 

 

Thanks

 

Link to comment
Share on other sites

i figured out that I needed to define a variable outside of the class

 

Like this:

<?php 
require_once(LIB_PATH."/".'initialize.php'); 

$file =  SITE_ROOT . "/logs/log.txt"; 
  
  class logger {
  
  
  
  static function log_action($action="", $message = "") {
			global $session;
			global $file;

		  $handle = fopen($file, 'at');
		  fwrite($handle, date("Y-m-d H:i:s"). "| ". $action . " " . $message. "\n");
		  fclose($handle);	
	  }
	  
  }
  
  
?>

 

But I don't understand why if I have defined a constant I can't use it in a class.  Is there a way to do this because I would prefer to keep $file private.

 

Thanks.

 

Link to comment
Share on other sites

When declaring a class property, the value must be constant, ie. it can't depend on run-time information.  From the PHP manual:

<?php
class SimpleClass
{
   // invalid property declarations:
   public $var1 = 'hello ' . 'world';
   public $var2 = <<<EOD
hello world
EOD;
   public $var3 = 1+2;
   public $var4 = self::myStaticMethod();
   public $var5 = $myVar;

   // valid property declarations:
   public $var6 = myConstant;
   public $var7 = array(true, false);

   // This is allowed only in PHP 5.3.0 and later.
   public $var8 = <<<'EOD'
hello world
EOD;
}
?>

Link to comment
Share on other sites

So I just did this:

$file =  SITE_ROOT . "/logs/log.txt"; 

 

like this:

<?php 
require_once(LIB_PATH."/".'initialize.php'); 


  
  class logger {
  
  
  
  static function log_action($action="", $message = "") {
			global $session;

			$file =  SITE_ROOT . "/logs/log.txt"; 

		  $handle = fopen($file, 'at');
		  fwrite($handle, date("Y-m-d H:i:s"). "| ". $action . " " . $message. "\n");
		  fclose($handle);	
	  }
	  
  }
  
  
?>

 

I'm not clear on why this works.  Why does

$file = ...

work but

private static $file = SITE_ROOT . "/logs/log.txt"; 

doesn't?  I understand what Cagecrawler posted...but what is the difference between the two variables.  Clearly they are being defined differently but I'm not totally understanding how PHP views the two definitions.

 

Thanks for your  help.

 

Link to comment
Share on other sites

In your example, $file is simply a variable within the function.  It only exists within the scope of the function (ie. you couldn't use $this->file).  When you define it outside of the function but inside the class (as private static) PHP doesn't see it as a normal variable but as a property of the class.  You can now use it anywhere in the class (as $this->file) and any value you give it will persist as long as the instance of the class remains.

Link to comment
Share on other sites

So I thought I was cool but what do I do if I want the variable to be a class attribute that is usable by the rest of the functions in the class so I only have to load the path once?

 

I tried this: 

<?php 
require_once(LIB_PATH."/".'initialize.php'); 



class logger {

private static $file;

	 function __construct() {
		static::$file =  SITE_ROOT . "/logs/log.txt";
	}

	static function log_action($action="", $message = "") {
		global $session;



		$handle = fopen( static::$file, 'at');
		fwrite($handle, date("Y-m-d H:i:s"). "| ". $action . " " . $message. "\n");
		fclose($handle);	
	} // End of log_action() function

	static function log_exists() {

		if (!static::$file) {

		}

	} // End of log_exists() function

	static function read_log() {

	}  // End of read_log() function



} // end of logger class


?>

 

But it didn't work, meaning that it didn't write to the file.

 

Link to comment
Share on other sites

Remove the static keyword on $file, and then refer to it as $this->file rather than static::$file (which should actually be self::$file anyway).  Only use static properties if you want to access them independantly of the class which the private property would block anyway.

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.