Sunday, November 4, 2007

PHP and Classes

The simplest way to learn about classes in PHP:
Well let's start by defining what is a class:
A generalized category in object-oriented programming that describes a group of more specific items called objects.
A class provides a template for defining the behavior of a particular type of object. Objects of a given class are identicalto each other in form and behavior.
A class is a descriptive tool used in a program to define a set of attributes or servicesthat characterize any member (object) of the class.
Now let's define a class in PHP and use some of the objects function. Let's assume that our object is an associative array having its indexes named after a product table. having fields: ID, NAME, DESCRIPTION.
So we should start by creating the table called products:
CREATE TABLE `products` (
`id` TINYINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 20 ) NOT NULL ,
`description` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `id` )
)

CODE

//let's define our class, and a set of functions inside this class.
class productdb
{
//this function create an empty instance of an associative array
function EmptyObject()
{
$product=array('ID'=>NULL,'NAME'=>NULL,'DESC'=>NULL);
return $product;
}
//this function takes a primary key as an input and returns the complete set of values corresponding to that primary key
function GetRow($id)
{
$result=mysql_query("SELECT id,name,description from products WHERE id='$id'");
while($row=mysql_fetch_assoc($result))
{
$product['ID']=$row['id'];
$product['NAME']=$row['name'];
$product['DESC']=$row['description'];
return $product;
}
}
}
//Now let's learn how to use those classes....
//first let's create an instance of that class....
$myobj= new productdb;
//now let's create an empty object, that has the specification of the object described above...
$myproduct=$myobj->EmptyObject();
//Suppose our product table is filled with values....
$myproduct=$myobj->GetRow($id)
echo $myproduct['ID'] . $myproduct['NAME'] . $myproduct['DESC'];
?>
And basicaly that is the simplest way to understanding classes and using them.

Validity of username and passwords

We have assumed that you have created a table called "users", and that table contain as much fields as you want, you can insert recordsinto that table using a form in html. But be sure to include two fields:
name= it is the username of the user
password= it is the password of each user
Simply create a form, using html, and set the action of that forum to a php file where you will include this small piece of code:

  1. //First lets get the username and password from the user
  2. $username=$_POST["username"];
  3. $password=$_POST["password"];
  4. //Second let's check if that username and password are correct and found in our database
  5. $sql1=mysql_query("SELECT name, password FROM users WHERE name='$username' AND password='$password'")
  6. if (mysql_num_rows($sql1)==0 mysql_num_rows($sql1)>1)
  7. {
  8. echo "Sorry, the username and password you submitted are not present in our database";
  9. }
  10. //if there are found in our database, and there is only one occurence of that username and password
  11. //thus making them valid, so inside, you can include the webpage you want to open
  12. if(mysql_num_rows($sql1)==1){include("the webpage");
  13. //open up the secure page
  14. //instead of "the webpage" type in the path your secure website is located in
  15. }
  16. ?>
I know it is a beginner level way to restrict access to a webpage, but it seems to work fine, for a single webpage. Check it out, might be useful.

File-upload-with-PHP

Here are the steps :

1. Set up an html page with a form.

2. upload the file to the server.

3. Move the file to it's destination.

4. Let the user know if the upload was successful or not.

http://www.weberdev.com/ViewArticle/File-upload-with-PHP

Tuesday, August 28, 2007

How Do You Use the Internet?

This winter I had the pleasure of attending June Hollister's Largo Workshop class, which completely changed my understanding of the Internet. Prior to taking this class, I had been wasting time using the Internet for political purposes, researching, and emailing. When I explained this to June, she recommended several changes for me to make better use of the Internet.
First, she explained I really needed to obtain DSL service. For over a half a decade, I had a dial-up Internet service provider and was very hesitant to make changes. I didn’t want to be bothered with changing email addresses, and I wasn’t sure if DSL would work even if I could figure out how to properly install the equipment. However, I am relieved to say the installation process was well documented, easy-to-follow, and Verizon DSL works better than I thought it would. Now, I am able to download faster, to navigate quicker, and have more viewing options. In addition, I am very pleased to actually be able to use my phone because it is not tied up for hours on end anymore.
Second, the change of email address turned out to be a blessing. June had recommended Google’s extras in class, so I sent the account I closed into my Google’s Gmail account because it has a Spam folder. From here I sorted through the Spam and determined which emails to have sent to my new Internet account. I unsubscribed from dozens of emails which were wasting my time and energy, consequently freeing up hours of my time. Now, it is a relief to not have to wade through a hundred emails everyday.