PHP OOP __set issues with $_POST data

Soldato
Joined
4 Mar 2010
Posts
5,038
Hi chaps I am coding a form class, I am having a problem using $_POST[] as a parameter in the __set method; it is throwing a syntax error " unexpected '[' ".

I suppose I could declare another variable with the post value then set it, but it seems an inefficient way of doing things.

Any suggestions?

Oh also any good tips about cleaning data before it goes to the db??

Luke
 
Okay sure thing :)

PHP:
<?php

class admin
{
	

	
	public function __construct($loggedin,$username,$password,$capchta)
	{
		session_start();
		$this->loadHeader();
		$this->hasLoggedIn($loggedin,$username,$password,$capchta);
	}
	
	public function __set($username, $_POST['username'])
	{
		$this->$username = $_POST['username'];
		
	}
	
	public function __destruct()
	{
		
		echo '</div>
			  </body>
			  </html>';
			  
	}
	
	public function loadHeader()
	{
		
		?>
    	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Admin panel</title>
		<link rel="icon" href="../images/site/favicon.ico" type="image/x-icon" />
		<link rel="shortcut icon" href="../images/site/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" type="text/css" href="http://<?php echo $_SERVER['SERVER_NAME']; ?>/includes/css/adm.css" />
		</head>
		<body>
		<div id="content">
		<?php
		
	}

	private function hasLoggedIn($loggedin,$username,$password,$capchta)
	{
		if($this->loggedin == false)
		{	
			$this->checkLogDetails($username,$password,$capchta);
		}
		else
		{echo "<p>you are logged in, redirecting you to menu now...</p>MENU!!!";}
		
	}

	private function checkLogDetails($username,$password,$capchta)
	{
		echo "checkLogDetails reached\n";
		$this->areDetailsSet($username,$password,$capchta);
	#$this->sanatise();
	#stripslashes();
	#$this->checkdb();
		
	}
	
	private function areDetailsSet($username,$password,$capchta)
	{
		echo "areDetailsSet reached\n";
		if(!isset($this->username) || !isset($this->password))
		{
			$this->loadLoginForm("Both fields need to be filled");
			exit;
		}
		else
		{
			$this->doesCaptchaMatch($capchta);
		}
		
	}
	
	private function doesCaptchaMatch($capchta)
	{
		echo "doesCaptchaMatch reached\n";
		$securimage = new Securimage();
		
		if ($securimage->check($this->capchta) == false)
		{
			echo "doesCaptchaMatch Loop reached\n";
			$this->loadLoginForm("The captch acode you entered was incorrect.");
			
		}
		
	}
	
	
	private function sanatise($username ,$password)
	{
		if(!ctype_alnum($username) || !ctype_alnum($password))
		{
			$this->loadLoginForm("<p>Invalid characters used in username or password</p>");
			$_SESSION['loggedin'] == false;
		}
		elseif(checkLengh($username ,$password) == false)
		{
		 	loadLoginForm("<p>Login details too long or too short</p>");
			$_SESSION['loggedin'] == false;	
		}
		
	}
	
	private function checkLengh($username,$password)
	{
		if(strlen($username) > 20 || strlen($password) > 20 || strlen($username) < 8 || strlen($password) < 8)
		{
			
			return false;
			
		}
		
	}
												   
		
	
	public function loadLoginForm($error)
	{
		
		echo'<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="log" >';
		if(isset($error))
		{
			echo $this->error;
		}
		$this->loadLogfields();
		$this->LoadCaptcha();
		echo '</form>';

	}
	
	
	private function loadLogfields()
	{
		?>
		<fieldset>
		<legend>Login</legend>
		<label for="nam">Username: </label><input type="text" id="nam" name="username" value="<?php if(isset($_POST['username'])){echo $_POST['username'];} ?>"  />
		<label for="pas">Password: </label><input type="password" id="pas" name="password" value="<?php if(isset($_POST['password'])){echo $_POST['password'];} ?>"  />
		</fieldset>
		<?php
		
	}
		
	private function LoadCaptcha()
	{
		?>
		<fieldset>
		<legend>Capcha And Submit</legend>
		<p>Please enter the string as you see it.</p>
		<label for="captcha_code">Capcha: </label><input type="text" name="captcha_code" size="10" maxlength="6" />
		<img id="captcha" src="/includes/securephpcapchta/securimage_show.php" alt="CAPTCHA Image" /><br />
		<a href="#" onclick="document.getElementById('captcha').src = '/includes/securephpcapchta/securimage_show.php?' + Math.random(); return false">Reload Image</a>
		<input type="submit" value="Submit" />
		</fieldset>
		<?php
		
	}
	
	
}
?>
 
Thanks for the link. Yeah sorry when I initiate the class I do this..

PHP:
$form = new admin($_SESSION['loggedin'],$_POST['username'],$_POST['password'],$_POST['capchta']);

That should parse the data over to the constructor then to __set ,right?

Luke
 
Well I got it working fella, had to tinker with one method as it wasn't spitting out an error message I missed adding a $this->error; all seems to be working fine now, thank you!

With me and coding I started with PHP made a load of basic stuff then went to C# because I wanted to learn XNA to make some homebrew games and now I am back with PHP trying to get it to work nicely.

From reading what you have just posted I suppose my class is too big and should be broken up into say login, capcha and validation classes?

I wasn't too sure how small to break everything up and I liked the idea of it being one module ;) Starting in procedural it is really kinda tricky to change the way I think about how programs should be laid out you, know what I mean?

Well enough about me :) what are you doing with C, anything interesting??

Luke
 
Yeah XNA is great!

I made a very simple pong game setup with very little assistance, once you know what your doing with drawing to the screen and controls the rest isn't too bad. Think I am going to code a space invader or tetris clone next.

C# is an absolute pleasure to work with.

But at the moment I am grinding with the PHP, just kind of in a lul for inspiration about projects or apps I could make..
 
Back
Top Bottom