Join 136,479 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,632 people online right now. Registration is fast and FREE... Join Now!
I'm trying to create web pages using this new method I've found. However, I can't seem to fully percieve the full use of them. I understand (Well sorta), how to use one...
CODE
class userlogin { var $username; var $password; function filtervariables() { $this -> username = stripslashes($_POST['username']); $this -> password = stripslashes($_POST['password']); } function checkinput() { if(!$this -> username || !$this -> password) { print "Please enter your username and password before logging in. <br>"; } else { $sql = "SELECT * FROM users"; $sqlquery = mysql_query($sql); $count = 0; while ($count = 0; $count++); if($count == "1") { $_SESSION['user'] = $this -> username; setcookie("asfagag", "asfasf", time()+3600); } else $_SESSION['user'] = "0" } } }
Anyway, could someone explain to me, the use of classes? I mean, the do's and dont's, the rules (I.e. the limits and stretches of a class), the requirements to call one, the implementation of user inputs into a class (See above attempt), mysql inputs (I.e. setting a tablename in another class; recalling it in another); basically how to use them properly...
Well classes are used in a method of programming known as Object Oriented Programming, or OOP. OOP is a relatively new (i.e. past few years) concept that has been emerging as a great way to program. The point of OOP is to basically modularize your entire site. It makes code reusability easier and allows you to protect data. C++, Java, and PHP 5 are examples of languages that utilize OOP.
Entire books have been written on OOP because it's such a broad topic. All the questions you have would really require a lot of explanation to get across. I can highly recommend a book on PHP OOP though that may get you on your way. It's called Object Oriented PHP and can be found at:
Well classes are used in a method of programming known as Object Oriented Programming, or OOP. OOP is a relatively new (i.e. past few years) concept that has been emerging as a great way to program. The point of OOP is to basically modularize your entire site. It makes code reusability easier and allows you to protect data. C++, Java, and PHP 5 are examples of languages that utilize OOP.
Entire books have been written on OOP because it's such a broad topic. All the questions you have would really require a lot of explanation to get across. I can highly recommend a book on PHP OOP though that may get you on your way. It's called Object Oriented PHP and can be found at:
function login() { $sql = "select * from Registered_Users where username = '$this->username' and Password = '$this->password'"; $result = mysql_query($sql) or die ( mysql_error() );
if ($user->login()) { // Whatever you want the success code to be } else { // Whatever you want the fail code to be }
Notice I have a function named User. This is called when the user is instantiated. You pass in the username and the password. This sets those variables to the class.
The following would go on your index page
php
$user = new User($_POST['username'], $_POST['password']); if ($user->login()) { // Whatever you want the success code to be } else { // Whatever you want the fail code to be }
Typtcally you would sanitze your data before sending it through to the class, but I covered that in the class with the mysql_real_escape_string() function.
Really study my code and compare it to yours to see where the faults are. Key points to notice are that I pass values into the class. I'm only keeping track of hte username and password. I sanitize my data. I return a value from the function rather than doing all the success fail/code in there.
Also, when I instantiate, I use $user->login(username, password). Note there are no space between the variable and the function. I'm not sure if PHP picks up on that but it might.
Those are just a few notes. Keep studying OOP. You're getting close but you're not quite there. Take it easy.
Hey thanks man!! I really appreciate all the help!! However, I'm still stuck
How would I create a checklogin or registration function? I've tried to do so, but it doesn't seem to work...
CODE
class login { function ($username, $password) { $this -> username = mysql_real_escape_string(stripslashes($username)); $this -> password = mysql_real_escape_string(stripslashes($password)); //how would I check if the user has submitted it? I mean, if empty, return a message, else login the user. Same for a registration technique; return message if username is x amount of characters in length, else, register user.
On the index page, $user = new User($_POST['Username'], $_POST['Password']); //Lemme go back to something....
Back to the objects page, var $username; var $password;
function filter_var($username, $password) { $this -> username = mysql_real_escape_string($username); $this -> password = mysql_real_escape_string($password); } function check_var($username, $password) { if(!$this -> username || !$this -> password) { print "You must enter a username and password in order to login. <br>"; } //else?? }
Anyway, back to index.php... We've already defined the username and password globals... So.. $user -> filter_var($_POST['Username'], $_POST['Password']); $user -> check_var($_POST['Username'], $_POST['Password']); //kinda lost now :(
I want to be able to return a value if the user enters a wrong value or just simply hits the submit button. However, if the post is fine, submit the details or login the user and gather the content.
I would prefer to use globals if they were to pass through each function consistently (I.e. if the first function were to filter them, the other functions view them as filtered variables?)
That should get you on your way to figuring it out. Also, your first few lines of code will not work
Make sure you plan out your class before you start typing away. Know precisely what you're trying to do, write it out on paper, then start coding it. Planning is the most important thing.
*sigh*... Thanks for the help, but I'm trying to percieve the use of classes. Here's a short example of what I'm attempting to committ...
CODE
//userobjects.php
<?php Class checkinput { //set a global variable, accessible through each function, saving me from recreating the variable everytime I create a function. var $username; function variables($var) { //variable will be altered to $_POST['username'] on the next page. The argument $var will tell the function to give the global a value of whatever is contained within $var. $this -> username = $var; } function filter_var($this -> username) { //I'm a little lost here... I'm trying to sanatize the variable. Since $var has altered the global, perhaps I could sanatize the global through the argument? Some help here please... mysql_real_escape_string(striptags(stripslashes($var))); } function check_var($this -> username) { //same here... :( if(!$var) { print "Please enter a username. <br>"; } else { print "Thank you <b>". $var. "</b> for submitting. <br>."; } } } ?>
Here's the index.php file...
CODE
<html> <body> <?php //if user hits the submit button... if($_POST['submit']) { //initiate a new instance of the class checkinput... $user = new checkinput(); //the global will then be valued $_POST['username'] through the function variables $user -> variables($_POST['username']); $user -> filter_var($var); //uhh... filter variable? $user -> check_var($var); //same } ?> <form action="index.php" name="forma" id="forma" method="post"> Username: <input type="text" name="username" id="username"> <input type="submit" name="submit" id="submit"> </form> </body> </html>
Hopefully you get that idea of what I'm trying to understand.