For those of you just learning, like me, PHP stands for Hypertext Preprocessor. I have yet to find out all of what PHP can do, but I'm looking forward to finding out.
If anyone has any resources that could help me, please share - I need all the help I can get.
Posted by: Justin - 11-10-2006 11:14 AM
- Replies (2)
As you know the Yahoo Publisher Network is in beta and only accepts U.S. traffic. If you send them non U.S. traffic you run the risk of getting your account banned. To do this you need to geo-target your ads so that users in the U.S. will see the YPN ads and visitors from anywhere else will see something different. A few people have asked me how to do this so I decided to write up a little tutorial.
First you will want to get a GEOIP database from the MaxMind website. I like to use the GeoLite Country database because it is free and still has a 97% accuracy rate. You can download the MaxMind database from http://www.maxmind.com/app/geoip_country and the latest version can always be found at http://www.maxmind.com/download/geoip/da...oIP.dat.gz . Try it with different proxies to test how accurate it is. Thats about all there is to it. You will have to update your GeoIP database every once in awhile to keep it up to date but other than that it need no maintenance. Of course you can always make it maintenance free by adding a cron script to update the DB for you but we will save that for another post Hope this helps
In this tutorial you will learn how to make a simple contact form. It re-directs to an error page if errors are found and checks the email address for any "bad characters."
PHP Code:
<?php if($submit=="Submit") { if(empty($mail)) header("Location: index.php"); elseif(!CheckMail($mail) || empty($message) || empty($subject)) { header("Location: error.php"); Die("Please go back and fill in a valid e-mail adress!"); } } ?>
This little code here gets the whole thing started. The if statement comes to life when the "Submit" button is clicked. If a form is left empty it will redirect to error.php, if it all goes well it will redirect to index.php. You can change these if you want.
This closes it up and tells it where to send the information. Change "YOUR E-MAIL ADDRESS HERE" to the email address you want the information to be sent to.
Posted by: Justin - 10-02-2006 02:33 AM
- Replies (1)
Learn a quick introduction to OOP (Object Oriented Programming). We will be creating a small class for fun to demonstrate how it all works.
In this tutorial we will be creating a class that will allow us to add up cars and then display the total weight of all the cars and also change the default weight per car.
So let's start the actual code part, for our script we will have 2 files. The first file we will be creating will contain our class, then the second file will include our class file and use our class to display the information we want.
Start of class.php
PHP Code:
<?php class CarCounter { var $cars = 0; var $weightPerCar = 500.0;
function totalWeight() { return $this->cars * $this->weightPerCar; } } ?>
I just gave you the total code for our car class, I will now break it down and explain it. First we must define our new class by simply putting class then the name of the class that you want to create.
Then we define a variable. We create a variable called $cars. We will be using this variable to define how many cars we actually have, so we set this to 0 because we dont have any actual cars at the moment. Next we define another variable called $weightPerCar. This is the default weight per car. I have set this to 500.0, just a fictional number. Later on we will be changing the default weight though. Now we define a function within our class, this function will be used to just add more cars to our total. So we rightfully call this function cars. Then all we are doing is giving our function cars an argument, this is where we let the user put in as many cars as he wants to be added, we use the variable $n which will be used later on in the same function, we set $n to a default of 1. Then all we do is add $n to the current total.
Now we define our totalWeight function. This is where we calculate the total weight of all our cars by using the default totalWeight that we defined earlier. This works the same way as our add function except we are multiplying cars by totalWeight to get the weight of ALL our cars.
Then we close off our function and class and we are done with class.php, now we move onto cars.php where we actually use our class.
print "With the new weight per car, your total weight is now " . $cars->totalWeight() . " kg"; ?>
This is where we actually use our class, now to use our class we must first include it in our file. That is where we use the require. Now that our class file is included we can now use the class we defined in that file. Now to use the class and the member variables defined in the class we must create a new instance of the class. We can define our instance to a variable to use in our script.
This is where we have -
PHP Code:
$cars = new CarCounter;
Once we have defined a new instance of our class we can now use the member variables defined in our class. So then we add 5 cars to our total.
Then we print what we have so far. Using our variable $cars, we can then access which member variable we need. cars was our member variable in our class that was set to 0. So now we set it to 5 to give it a value.
Now let's say we want to add more cars to our total we use our add function we created. So we use it the same way we would use a regular function except we must first say that it is in our class (Which is defined to our variable of $cars). So we add 10 more cars to our total so we now have 15 cars, and to prove this we print it out the exact same way we printed it out the first time.
Now with our 15 cars we want to find out the total weight of all 15 of these cars using the default weight we defined as one of our member variables. So we just do another print statement except we use the totalWeight function.
Now let's say we wanted to change the default weight of our cars, we can just give it a new value.
PHP Code:
$cars->weightPerCar = 700.0;
Then to prove that the weight actually did change we print out our totalWeight again except this time the function is using a new totalWeight default value so our total will be higher.
Posted by: Justin - 10-01-2006 07:17 AM
- Replies (5)
The include and require functions are handy for using files that contain other variables, functions, etc. What happens when you have a collision between function names though? Well quite simply you get an error.
Say you have a function called name in a file called functions.php.
PHP Code:
function name() { //do something or return a variable }
Now say you have file 1.php 2.php and 3.php and they all use this function and share some variables.
Finally we have file named index.php which uses the values.
PHP Code:
include ("1.php"); include ("2.php"); include ("3.php");
Well index.php will cause an error that the function name has be redeclared. How do we fix it? Very easily. require_once is a function similar to the require that was used yet it only requires the file to be required one time. Therefore there is only one function called name. include_once works similarly except that is including the file and the file isn’t required.
2. Now we start to build our forms in HTML. We'll start off with...
Code:
<form action="process.php" method="post">
The word "form" defines a form. Adding the word action defines where to send the data when the submit button is pushed.
the method="post" the ocntents of the form to be sent.
3. Below the code we just put in, you should now add the follwing code directly below it.
Code:
<input name="password" type="text" />
<br/>
The word "input" defines an input field. The little code "field text" let's us now it will be a text field.
So far you shoul have a cod ethat looks like this...
4. We will insert one more line of code below what we have now.
Code:
<input type="submit" value="Submit" />
Once again the word "input" defines an input field. Now, we have some new stuff in there. Type determines what it will do. In this case the type is Submit. It will make a button and when the button is clicked it will submit all of our data we have in the document already.
Next we have "value" value is just the text that is displayed on the submit button change it if you would like to.
6. Now we are going to start the PHP file. Open a new document and save it as process.php.
7. Start off with you basic php tags.
PHP Code:
<?php
?>
8. All we are using is an if/else code so I will post the whole PHP code and explain.
PHP Code:
<?php if ( $password == "yourpasshere" ) { // header redirect to page header( "Location: blah.php" ); //or you can use an echo then a JS redirect /* echo "Correct!"; */ }else{ header( "Location: blah.php" ) // echos the pass is wrong. you can use an echo with JS redirect or another header. echo('Password is incorrect! '); } ?>
If starts the if function. $password is the varible and the two = signs means that the the pasword must match the word in the quotes. So if you would like to change
the password just change the word inside the quotes. Do not remeove the quotes! Below the if we have an echo. This echo is shown if the user enters the correct password. You may
change anything between the quotes.
Now we see the else funtion. This simply means if anything but the password is entered it will do whatever the else function tells it to.
In this case we tell it to die. Die just simply means stop. Since die is a funtion we must have these after it (). Between the parentheses we cna put text
to be displayed if the password is incorrect. You can change anything in there between the ' marks.
We are done! Close your php tags upload and enjoy!
Note: This is my first tut, so it probobly isn't the most secure pasword protect in the world. I will update with a more securre version soon!
In this tutorial you will learn how to make a stat counter that diplays total unqiue hits, unique hits today, total hits, and total hits today.
1. First, we need to create a database to store the information. You will need a database table called stats. This can simply be made by pasting the following code into the query box under the SQL tab in PHPmyAdmin.
Code:
CREATE TABLE `stats` (
`date` varchar(255) NOT NULL default '',
`ip` varchar(255) NOT NULL default ''
) TYPE=MyISAM;
2. Now we need to have a file to connect to the database. Copy this code and fill in your information. Save it as db_info.php.
Code:
$conn = mysql_connect("localhost","username","password"); mysql_select_db(db name) or die(mysql_error());
Fill in the correct information and place between PHP tags.
This code allows information to be sent to the databse. That information being the users IP address and the total # of hits. Place this on the page where you want stats to be taken from.
4 . Now we are going to display the hit counter.
Code:
<?php
require ("db_info.php");
$date = date("M d, Y");
$uniquetotals = mysql_query("SELECT DISTINCT ip FROM stats");
$uniquetotal = mysql_num_rows($uniquetotals);
$tdayuniques = mysql_query("SELECT DISTINCT ip FROM stats WHERE date = '$date'");
$tdayunique = mysql_num_rows($tdayniques);
$hitstotaltday = mysql_query("SELECT * from stats");
$hitstotal = mysql_num_rows($hitstotaltday);
$tdayhits = mysql_query("SELECT * from stats where date = '$date'");
$tdayhit = mysql_num_rows($tdayhits);