next Posted July 15, 2008 Share Posted July 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/ Share on other sites More sharing options...
corbin Posted July 15, 2008 Share Posted July 15, 2008 Google variable scope. Also, are you sure you should be using $_REQUEST? Google $_POST and see if you really need to be using $_REQUEST. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590140 Share on other sites More sharing options...
next Posted July 15, 2008 Author Share Posted July 15, 2008 $_REQUEST can be used regardless of what method is used in a form. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590150 Share on other sites More sharing options...
corbin Posted July 15, 2008 Share Posted July 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590208 Share on other sites More sharing options...
KevinM1 Posted July 15, 2008 Share Posted July 15, 2008 To fix your errors not being output, use $this->errors. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590410 Share on other sites More sharing options...
next Posted July 15, 2008 Author Share Posted July 15, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590509 Share on other sites More sharing options...
KevinM1 Posted July 15, 2008 Share Posted July 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590543 Share on other sites More sharing options...
next Posted July 15, 2008 Author Share Posted July 15, 2008 Actually i see it the other way around, if you decide to change the way information is passed in a form you just need to change the method and $_REQUEST takes care of the rest, but i'm new to PHP to know all the cons. Btw, it worked, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590642 Share on other sites More sharing options...
Daniel0 Posted July 15, 2008 Share Posted July 15, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/114768-creating-a-user-class/#findComment-590811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.