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
		
	}
	
	
}
?>
 
I have not used __set myself, but its called when an undefined variable is assigned a value ( e.g. there is no var $bob in your class and you're assigning a value to $bob)

So you are miss using __set.

I would move the:

PHP:
$this->$username = $_POST['username'];

into the class constructor which is called when the class is created (eg using $bob = new classname(params); )

Also i would re look into OOP and classes as you aren't going about it the right way.

EDIT:

googled it and some more help: http://www.tuxradar.com/practicalphp/6/14/3
 
Last edited:
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
 
So username has already been passed in to your class.

what i would do is add:

PHP:
var $username; 
var $password;

at the top of your class above the __construct method.

then in the __constuct method:

PHP:
$this->username = $username;
$this->password = $password;

Then when you need to use the username in the class you can just say $this->username to get the current username. Which i guess is what you wanted to use the __set method for, which to be honest is confusingly named!
 
Well the idea of using classes is to reduce repeatedly coding the same bits over and over again. So you would have a class to do your recatchpa which can then be created and used in total abstraction to actually what the page does and not need re writing/copying to create the same functionality. As all the recatchpa is concerned about is whether the input is valid.

Then you would have a class or multiple classes to handle login and validation of login and user accounts etc so you can put a login anywhere on the site by just creating an instance of the class.

I personally only learnt about OOP concepts and haven't really got to use them properly as what i do now is all in C so you may get a better idea by looking through this thread: http://forums.overclockers.co.uk/showthread.php?t=18151385

Although the code is in java the descriptions and ideas and class names will probably make sense enough to follow it through if you don't know java.
 
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
 
I dunno about looking at it as too big just if you know you will need something again stick it in a class and keep it as abstract as seems feasible. I know what you mean about wanting to split it up sometimes it just feels like a needless expense, when it comes to PHP a lot of problems can be solves easily enough taking a procedural approach as its really how a web page works. I tend to use a bit of both really depending on what works best. So don't think of the number of lines and more over whether its better severed as an object. If you are to stick with PHP you will end up building a library of your own stuff and will be able to knock out a decent site without needing to rewrite the underlying functionality all the time.

Well myself, i am on work placement for uni and messing around with their API building test apps and example apps, nothing too interesting :(

How did you find XNA? I intended to pick that up with a nice big book for it and all but just don't have the will power after coding all day long :p
 
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