|
Samay Live | |
|
EBooks » Web Tutorials
It's the Information age, and information can't be contained, it grows by the second. Hence no
single site can suffice the needs of today's visitor be it Yahoo! or any other site. Internet is
a whole new medium and tracking of 100's of portal start ups is annoying, but still you wander
about here and there. Agreed that bookmarks make life much simpler, but easier hmm...
Today I'll discuss how to grab stuff from all these happening places, I'll take the example of
Yahoo! NewsPod, probably the most successful script for grabbing news from Yahoo!
The base methodology behind "grabbing" content is RegEx, which allows you to grab a certain
portion of the obtained content.
Linear Working :
(1) A socket (behind the scenes) connection to http://www.yahoo.com/index.html
(2) Reading a pre-determined (or filesize determined) bytes of the page and storing into array
(3) Searching for particulars and storing into another variable
(4) Sorting out further for extra customization There are many situations where you might want to display up-to-date information
on your website. I will use the process of displaying the latest headlines from Slashdot.org
as an example here.
Getting the headlines
The file http://slashdot.org/slashdot.xml
contains the current slashdot headlines for people to use on their websites. You could
just load the data from this file every time someone visits your page. However, Slashdot requests
that you only access the file once every 30 minutes or more (to reduce the load on their server).
This means we will need a better strategy for making sure we have up-to-date headlines.
You could use cron to perform the update once every 30 minutes but for various reasons this
might not be possible. My approach is to write a PHP script that does the following when a user loads
the page with the dynamic content:
1. Check whether a local (i.e. on my webserver) copy of slashdot.xml has been updated
within a predefined time period (more than 30 mins in this case).
2. If it has not been updated recently enough then download the current one from
slashdot.org.
3. Display the data from the local copy of slashdot.xml. Before I begin I just want to say:
I always thought classes were hard, they are not hard at all.
They are actuatly VERY easy to use and to learn.
They can also add alot more security to your site, they can help
optimize code for faster loading times and so on.
How do they add security?
Instead of connecting to a database for example, and querying the
database with the default functions, you can pass them through a class,
have it evaluate the query, see if its valid, check to see if it is
unaltered and make sure no one is trying to inject sql. 1. What it does
IP banning someone from your site will effectivly stop all access they have
to your website. Make sure you ban wisely :-P
2. The Code
The code is fairly simple, we will set the $banned variable using an array.
It is here were we will enter IP address we wish to ban. Then, using the global
variable $REMOTE_ADDR to give us the user's IP address, we can check if it is
in the $banned array. If not, we let them carry on. If it is found in the $banned
array, using the die() fuction, we can remove all access to the site from that
IP. 1. Purpose?
In some sites, it is essential to show a user's IP address, for various reasons.
It is not that hard however, to do this. With some very simple PHP we can achieve
showing someone IP with relative ease.
2. How-To
It is not that hard at all, less then 3 lines total.
|
| |
|