Php
November 7th 2009 // PHP // Permalink
PHP A to Z sorting script
Here is a quick way to filter records from a MySQL table using dynamically generated alphabet links. The MySQL table may look something like this:
| id | firstname | lastname |
| 1 | John | Smith |
| 2 | Joanne | Johnson |
And here is what the dynamically generated alphabet navigation would look like:
The script will do following:
- get the letter user clicked on
- execute MySQL query using that letter
- display filtered results (if no letter was clicked we will display all records)
getcustomers.php
/* Get the letter user clicked on and assign it a variable called $sort */
$sort = $_REQUEST['letter'];
/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */
if($sort = ""){
$qry= "SELECT * FROM tbl_customers ORDER BY lastname ASC " ;
}else{
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */
$qry = "SELECT * FROM tbl_customers WHERE lastname LIKE '$sort%' ORDER BY lastname ASC" ;
}
/* Notice the use of '%' wilde card in the above query "LIKE '$sort%'". */
//next step is to execute the query.
$execute = mysql_query($qry) or die(mysql_error());
/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */
echo "" ;
for ($i = 65; $i < 91; $i++) {
printf('%s | ',
$PHP_SELF, chr($i), chr($i));
}
echo "
" ;
/* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table.
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */
if(mysql_num_rows($execute)>0){
do{
echo "" .$result['id']. " " .$result['firstname']. " " .$result['lastname']. "
" ;
}while($result = mysql_fetch_assoc($execute));
}else{
echo "No customer found.
" ;
}
Do you like this or find it useful? Drop me a note or treat me to a double-espresso from my favorite coffee shop.
November 1st 2009 // PHP // Permalink
UHits - PHP proxy hits generator
This scripts uses free proxies to generate fake hits to specified website.
Script Files
- proxy.txt - is a text file containing all proxy information
- proxy.php - does all the work of generating scripts
Both files need to be saved in the same directory on your server.
proxy.txt
Create a text file called proxy.txt and add one proxy address with port number per line:
207.156.32.23:80 203.156.35.123:8080 154.166.32.23:444
Make sure to save this file in the same directory as proxy.php. Script is ran using Cron-jobs, so you'll need to know how to set one up on your server.
proxy.php
//Let's make sure no warrnings are displayed by PHP
error_reporting(0);
// Specify url that shoud receive hits
// make sure to include trailing slash "/" at the end of folders
$url_1 = "http://www.yourdomain.com/";
// Specify your server and port
$myserver = "yourdomain.com:80";
// Specify how many hits to receive every time the script is run
// Selecting more then 5 may not work
$randnr = 2;
//read proxy file
$proxies = file("proxy.txt");
//now we will get a random proxy address from the proxies.txt file
$getrand = array_rand($proxies, $randnr);
for($x=0;$x<$randnr; $x++){
//setting time limit to zero will ensure the script doesn't get timed out
set_time_limit(0);
//now we will separate proxy address from the port
$PROXY_URL=$proxies[$getrand[$x]];
$proxyarr = explode(":", $PROXY_URL);
$address = trim($proxyarr[0]);
$port = trim($proxyarr[1]);
//following code generates the header file
$headerinfo =array(
'User-Agent' => 'UHits/1.0 ('.$myserver.')',
'Accept' => 'image/gif, image/x-xbitmap, image/jpeg, image/png, */*',
'Pragma' => 'no-cache',
'Connection' => 'keep-alive');
reset($headerinfo);
//now we are ready to reach our website through the proxy
$fp=fsockopen($address,$port);
if($fp) {
fputs($fp,"GET $url_1 HTTP/1.1rn");
fputs($fp,"Host: $myserverrn");
reset($headerinfo);
while(list($header,$value)=each($headerinfo)) {
fputs($fp,"$header: $valuern");
//let's give the script some time to execute
sleep(2);
} //end while
fputs($fp,"rn");
fclose($fp);
} //end if
} //end for
Cron Job
Using cron jobs you can set up the script to execute every hour or minute or every 3 minutes. Here are some cron job examples.
GET http://yourdomain.com/proxy.php> /dev/null
or
wget -q http://yourdomain.com/proxy.php> /dev/null
Do you like this or find it useful? Drop me a note or treat me to a double-espresso from my favorite coffee shop.
Comments
December 20th 2009
Jaja - Parse error: syntax error, unexpected ';', expecting ')' in /var/www/htdocs/proxy.php on line 19
www.zedpic.com - if any one have great idea of increase rank in alexa then tell me please.
July 15th 2010
shit - fputs($fp,"$header: $valuern"); $url_1 = "http://www.yourdomain.com/"; where that functions goes?, should be write better if you want to give some lessons.

Comments
January 19th 2010
Ronnie - Line 5 should read: if($sort == ""){ Other than that works great, thankyou :)
Reply
March 22nd 2010
sami - merci ça marche
Reply
September 5th 2010
Vince - on line 19, how do I pass an additional piece of information that is a php dynamic id? I have already tried: %s ...didn't work, it's gotta be a syntax error, help please?
emir - < a href="%s?letter=%s&id=id-goes-here" >%s< /a > or < a href="%s?id=id-goes-here&letter=%s" >%s< /a >
Reply