PHP - Class Advice
I have made a small class to learn how to do them. This basically adds the new user to the database upon registration. I would like to know if i am doing things right or if there is a better way. Also can anyone tell me why i would use this class? Previously I just included the insert query and if statements in my main register.php page and everything worked fine. what are the advantages to using it in a class?
Here is the class: <?php define ("NO_USERNAME", "Please Enter A Username"); define ("NO_PASSWORD", "Please Enter A Password"); define ("NO_EMAIL", "Please Enter An Email Address"); define ("SUCCESFULL_REGISTRATION", "Done"); define ("NO_MATCH", "Your Passwords Did Not Match"); define ("UNKNOWN_ERROR", "An Unknown Error Occured"); class User { public $user_name; public $password; public $email_address; public $password_confirm; public $processed; public $error; function user_create($user_name, $password, $email_address, $password_confirm) { if (empty($user_name)) { $this->error = NO_USERNAME; } elseif (empty($password)) { $this->error = NO_PASSWORD; } elseif (empty($email_address)) { $this->error = NO_EMAIL; } elseif ($password != $password_confirm) { $this->error = NO_MATCH; } else { $this->error = NULL; } if ($this->error == NULL) { echo "Username: " . $user_name . "<br />"; mysql_query("INSERT INTO test_members (user_name, password, email_address) VALUES ('$user_name', '".md5($password)."', '$email_address')") or trigger_error("SQL", E_USER_ERROR); $this->processed = TRUE; } } public function encryptPassword($password) { $password = sha1(md5(md5(sha1(sha1($this->password))))); } } ?> Similar TutorialsI have mysqli object in Database class base: [color=]database class:[/color] class Database { private $dbLink = null; public function __construct() { if (is_null($this->dbLink)) { // load db information to connect $init_array = parse_ini_file("../init.ini.inc", true); $this->dbLink = new mysqli($init_array['database']['host'], $init_array['database']['usr'], $init_array['database']['pwd'], $init_array['database']['db']); if (mysqli_connect_errno()) { $this->dbLink = null; } } } public function __destruct() { $this->dbLink->close(); } } Class derived is Articles where I use object dBLink in base (or parent) class and I can't access to mysqli methods (dbLink member of base class): Articles class: require_once ('./includes/db.inc'); class Articles extends Database{ private $id, .... .... $visible = null; public function __construct() { // Set date as 2009-07-08 07:35:00 $this->lastUpdDate = date('Y-m-d H:i:s'); $this->creationDate = date('Y-m-d H:i:s'); } // Setter .... .... // Getter .... .... public function getArticlesByPosition($numArticles) { if ($result = $this->dbLink->query('SELECT * FROM articles ORDER BY position LIMIT '.$numArticles)) { $i = 0; while ($ret = $result->fetch_array(MYSQLI_ASSOC)) { $arts[$i] = $ret; } $result->close(); return $arts; } } } In my front page php I use article class: include_once('./includes/articles.inc'); $articlesObj = new articles(); $articles = $articlesObj->getArticlesByPosition(1); var_dump($articles); [color=]Error that go out is follow[/color] Notice: Undefined property: Articles::$dbLink in articles.inc on line 89 Fatal error: Call to a member function query() on a non-object in articles.inc on line 89 If I remove constructor on derived class Articles result don't change Please help me I have an existing instance of my class Database, now I want to call that instance in my Session class, how would I go about doing this? Hi Can you call Class A's methods or properties from Class B's methods? Thanks. Ok. I know you can pass the object of a class as an argument. Example: class A { function test() { echo "This is TEST from class A"; } } class B { function __construct( $obj ) { $this->a = $obj; } function test() { $this->a->test(); } } Then you could do: $a = new A(); $b = new B($a); Ok so that's one way i know of. I also thought that you could make a method static, and do this: (assuming class A's test is 'static') class B { function test() { A::test(); } } But that is not working. I'd like to know all possible ways of accomplishing this. Any hints are appreciated. thanks If a class has a constructor but also has a static method, if I call the static method does the constructor run so that I can use an output from the constructor in my static method? --Kenoli Hi, I need to be able to call a class based on variables. E.G. I would normally do: Code: [Select] $action = new pattern1() but i would like to be able to do it dynamicaly: Code: [Select] $patNum = 1; $action = new pattern.$patNum.() Im wondering if that's possible? If so what would the correct syntax be? Many Thanks. I have two classes: ## Admin.php <?php class Admin { public function __construct() { include("Config.php"); } /** * deletes a client * @returns true or false */ function deleteClient($id) { return mysql_query("DELETE FROM usernames WHERE id = '$id'"); } } ?> ## Projects.php <?php class Projects { public function __construct() { include("Config.php"); $this->admin = $admin; $this->dataFolder = $dataFolder; } /** * Deletes a project * @returns true or false */ function deleteProject($id) { $root = $_SERVER['DOCUMENT_ROOT']; $theDir = $root . $this->dataFolder; $sql = mysql_query("SELECT * FROM projectData WHERE proj_id = '$id'"); while ($row = mysql_fetch_array($sql)) { $mainFile = $row['path']; $thumb = $row['thumbnail']; if ($thumb != 'null') { unlink($theDir . "/" . substr($thumb,13)); } unlink($theDir . "/" . substr($mainFile,13)); } $delete = mysql_query("DELETE FROM projectData WHERE proj_id = '$id'"); $getDir = mysql_query("SELECT proj_path FROM projects WHERE id = '$id'"); $res = mysql_fetch_array($getDir); rmdir($theDir . "/" . $res['proj_path']); return mysql_query("DELETE FROM projects WHERE id = '$id'"); } } ?> How can I call deleteProject() from within Admin.php? Hi people! class FirstOne{ public function FunctionOne($FirstInput){ //do stuff and output value return $value1; } } Then:- class SecondOne{ public function FunctionTwo($AnotherInput){ //do stuff and output value return $value2; } } What I want to know is this, if I want to use FunctionOne() in Class SecondOne do I do it like this:- (Assume as I have instantiated the first class using $Test = new FirstOne(); ) class SecondOne{ function SecondedFunction(){ global $Test; return $Test->FunctionOne(); } public function FunctionTwo($AnotherInput){ //do stuff and output value return $value2; } public function FunctionThree(){ //some code here $this->Test->SecondedFunction();<--I think as I can omit the $this-> reference } } My point is: Do I have to do it this way or is there way of having this done through __construct() that would negate the need for a third party function? I have a version working, I just think that it is a little convoluted in the way as I have done it, so I thought I would ask you guys. Any help/advice is appreciated. Cheers Rw Right now I have a SESSION so when users flip though pages they carry their info with them, what I'm trying to do now is that userhome.php can't be accesses unless the user just was succesful in cracking there system.. game I'm creating for those of you helping and following me while I do this! it's a virutal hacking simulation and where I'm now is that the user's passwordcracker was compared to the target systems 'systemkey' and either granted him access or didn't, if it did it displayed a progress bar then fowarded to userhome.php where the target users info will lay, right now though if I just type in userhome.php i get there without haveing the crack it.... any ideas? Hello, can someone please advice me on what scripts I will need to accomplish the following. I want users to be able to login to their personal page, on there will be items such as pdf files, jpeg files etc, that they will be able to download. Are there any free scripts out there that can do this, that anyone knows of? I don't mind paying if its a cheapish script for one of you to make for me, but money is a bit tight at the moment so a free script would be my 1st choice... Thanks for all your help So here's my problem I'm not sure how to approach this: I have a table with user_items which are stored together separated by commas. Code: [Select] 13,12,11,9,27,15,16,22,21,23,24,26,29,30,31,32,33 Now, I have a script where the user is in a trade and I want to verify the item they are trying to trade, but is there an alternative other than grabbing all of that users' items and checking that one item with all of the records? I've tried using Code: [Select] SELECT * FROM MYTABLE WHERE user_item_id IN(33) As an example to see if it will pull the rows with that ID. It didn't seem to work, am I doing it wrong? if so, forgive me. Any suggestions/help? The main problem is I don't want to have to explode that data and use a foreach to check that one item against all of that users items, as they could have well over 500. for those of you who don't know i am creating a piece of forum software called ASF. Ive done it by myself so far but as it grows i find it harder to write the code and keep organised. my code is a mess and things arent done the way they should be. So if anyone can give me advice or wants to help i could post some of the files for download. Even if you just want to have a look and let me know waht you think. Thanks Carl http://www.thevault.cz.cc In short i want to use the following code below, when someone selects there option and submits it, it would bring up details from the database on this user from the selected table, can you explain what it would be called doing this so i can look it up, Sorry to be a pain, Cheers. Code: [Select] <select name="target2" id="target2"> <option value=""></option> <?php $sql = "SELECT player_id, friend_id, name, is_active FROM contacts as c JOIN players as p ON c.friend_id = p.id WHERE c.player_id = $playerID AND is_active = 1 ORDER BY name ASC"; $que = mysql_query($sql) or die(mysql_error()); while($list = mysql_fetch_array($que)) { ?> <option value="<?php echo $list['friend_id'] ?>"><?php echo $list['name'] ?></option> <?php } ?> </select> Hi all, I am looking as a pet project to develop a review site, with the info stored in a database by id and the information grabbed bet get id and then displayed on a dynamic page, eg review.php?id=1 My question is this, if i throw keywords into the mix for each review, will search engines cache a review like this? Or would I need static pages for google etc to find the info? Thanks Hi, basically i have data in my database i want to represent as cash, i currently put the dollar sign infront of each echo which is fine, but how would i go about adding , to the php code itself as you cannot do this from the sql database.. I've been trying to find a good, up-to-date source on how to secure the authentication credentials for my db connection. I've done some PHP coding and would like to learn more. There's plenty information available, but I often find books inevitably have typos in the code. Also most of the online tutorials are either at least several years old or deal more with user login security. User authentication is one thing, but what are the best ways to secure the connection to the database itself? Obviously your basic newbie method of unencrypted host, username, password, and database stored in a connectvar file is just open invitation--or maybe not since it doesn't present a challenge to a hacker. Some say to encrypt the credentials with something like MD5 and store them in .htaccess. Other sources say not to use MD5. Any advice on where to find some good resources on this? Cheers! I have a restricted page for members of a website. This restricted area is within a directory called 'download.' There is a login form on two pages (home and support pages, found in the main menu). These pages are on the site root directory. When the user successfully logs in they are taken inside the download directory to index.php. This index.php has a different look to the site root design. I have since redesigned this page to have the same structure as the site root pages. I would love for the user to able to navigate around the main site if they wanted, and when they clicked support in the menu they would have all the download files there on the page, instead of a login form. My question is how would I implement this login so that when the user logs in the support page changes from the login page to the page with the files. I don't want to to duplicate the site within the download directory, I was hoping for an efficient method, but I am unsure how to go about it. Hello all I wonder if someone can give me some advice, sorry if this is in the wrong place, I couldn't seem to find the right forum to put it in. I am helping a friend to build a PC support ticket system for use by around 100 support groups, 50 schools and 20 universities, over time a total potential student population of between 300,000 and 800,000 might use it. Of course they as not all going to be accessing the system at once, but of something major happens, there could be almost half the students submitting a ticket at once, but I want to build it in a way that it could cope with 1,000,000 users, I know server space maybe expensive and luckily I wont have to pickup the bill for that. So my questions are; Can PHP handle this volume of traffic with multiple SELECTS, INSERTS etc Can PHP ever crash due to overload or is it always the server which has the overload and crashes, if it's PHP, what can be done to stop that? What would be the best way to hold user data? Is is best to spread the data over multiple tables Is there anything I should consider in PHP when working out a viable system Any advice on the type and level of server we should use I assume PHP would be the best to use as companies like Facebook seem to use PHP and they handle 500 millions users, so I guess it's stable enough. I guess Cloud hosting would be best to balance the load?? I was going to code this all myself over time and learn advanced PHP a bit more, is this a good idea or should I be learning using a framework like ZEND or something like Drupal? Thanks all Hi everyone This is not really about php code...sorry. But i want some advice if you dont mind. I am working on a system, but i would like it to be available for PC use, aswell as for mobile use. What would be the best? To create two websites, one for mobile other for PC, and upload them to .mobi and .com domains, or should i create only one... I need this to be as user-friendly as possible...because the clients who are going to use this, is those ppl that is not comfortable with a PC, not to mention the web. Thanks Hi All, I currently have a ticketqueue that show's all tickets assigned to a group of people, but split into personal queues, but the way that I wrote it, means that it needs manually updating if a specific person leaves/joins the department. For example, to get the queue details, I use the following query: Code: [Select] $username1 = mssql_query("select id,subject,body,priority from queue where assignedto = username1";) $username2 = mssql_query("select id,subject,body,priority from queue where assignedto = username2";) I have repeated this code for all of the users in our team. Which seams a waste, as I have all the information on our team stored in a DB called "sysadminusers". Is there an array I could use that would look at all the usernames in the table, and then repeat the query for me? I would also need this array to display the results on the page, currently I use the following: Code: [Select] while($username1_tickets = mssql_fetch_assoc($username1)){ echo $username1_tickets['id'],$username1_tickets['subject']$username1_tickets['body'],$username1_tickets['priority'];} while($username2_tickets = mssql_fetch_assoc($username2)){ echo $username2_tickets['id'],$username2_tickets['subject']$username2_tickets['body'],$username2_tickets['priority'];} I am just looking for some design advice and code examples that would help me tidy up my code for this page, it seams a lot of code for quite a simple page. Thanks Matt |