Jump to content

Recommended Posts

I'm trying to figure out how to create a User class, but i'm stuck and have no clue how to proceed.

I'm currently stuck with "register" method and it's error display. Here's what i have:

User class:

<?php
class User {
	private $user_name;
	private $password;
	private $keep_logged_in;
	private $found;
	public $errors;

	public function __construct($user_name, $password) {
		$this->user_name = $user_name;
		$this->password = $password;
	}

	public function logIn() {
		$found = Database::getRow("CALL user_exists('$this->user_name', '$this->password')");
		if($found) {
			session_register($this->user_name);
			$_SESSION['user_name'] = $this->user_name;
		}
		header('location: index.php');
	}

	public function register() {
		$found = Database::getRow("CALL name_taken('$this->user_name')");
		if($found)
			$errors .= "<li>user name is taken.</li>\r\n";
		if(strlen($this->user_name) > 50)
			$errors .= "<li>user name is to long</li>\r\n";

		if(!$errors) {
			Database::execute("CALL user_register('$this->user_name', '$this->password')");
			header('location: index.php');
		}
		else
			$this->displayErrors();
	}

	public function displayErrors() {
		echo "<ul>\r\n";
			echo $errors;
		echo "</ul>\r\n";
	}
}
?>

Registration page:

<?php
require_once('header.php');

if($_REQUEST['submit']) {
	$user_name = trim($_POST['username']);
	$password = trim($_REQUEST['password']);

	$user = new User($user_name, $password);
	$user->register();
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" id="registration">
<label for="username">User name:</label><input name="username" type="text" id="username" size="15" maxlength="50" />
<label for="password">Password:</label><input name="password" type="password" id="password" size="15" maxlength="100" />

<input name="submit" type="submit" id="submit" value="register" />
</form>

 

Script does detect if user name is already taken or if it's to long, but it doesn't display errors, how can i fix it?

Also, does anyone know a good tutorial on such class?

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/114768-creating-a-user-class/
Share on other sites

Yeah, but that's why some people frown upon it.  You don't know if the data is coming from post, get, cookies, or what.  In this situation, I don't think it would be a big deal, but it's a bad habit in my opinion.  Guess I was mostly pushing personal preference on that one.

corbin, yeah i usaly prefer to be more specific with global too, but this time decided to try out $_REQUEST. I tested it, and it does pass necessary info.

Nightslyr, thanks i'll try it out!

 

I don't think that corbin's comment was based on $_REQUEST not working, but rather on it being somewhat vague compared to the specific super-globals $_GET and $_POST.  In terms of function, it doesn't really matter, but semantically it's different than the other two options.  Not a big deal if you're the only one working on the code, but if someone needs to track if values are being passed in by GET or POST, it may cause a problem.

 

As for the other, just remember that when you want to access an object's properties or methods from within the object itself, you need to use the 'this' keyword.  Otherwise, you're merely creating a local variable.

POST is used when you are POSTing information to the server and GET is used when you are GETting information from the server. I'd say it's pretty simple and I don't see why you would "change the way information is passed".

 

An example of a POST request could be the one I will do when I finish typing this post. An example of a GET request could be searching or viewing a topic.

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.