Get url via http-proxy in php+curl

Solution:

  1. function GetData($url, $proxy='', $vars=''){
  2.   $ch=curl_init();
  3.   curl_setopt($ch, CURLOPT_URL, $url);
  4.   curl_setopt($ch, CURLOPT_HEADER, 0);
  5.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6.   curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  7.  
  8.   if($vars!=''){
  9.     curl_setopt($ch, CURLOPT_POST, 1);
  10.     curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
  11.   }
  12.  
  13.   if($proxy!='')
  14.     curl_setopt($ch, CURLOPT_PROXY, $proxy);
  15.  
  16.   $str=curl_exec($ch);
  17.   curl_close($ch);
  18.  
  19.   return $str;
  20. }

How to use this:

  1. $url='http://site.com/some-url.php';
  2. $proxy='255.255.255.255:80';
  3.  
  4. // post variables
  5. $vars='var1=1&var2=text';
  6.  
  7. $txt=GetData($url, $proxy, $vars);
  8. echo $txt;

Tags: , , ,

DeTypo in PHP

Sometimes we need to detect mistypes :)
We can use google-search to make sure if given word is a mistype, here my solution:

  1. if($_GET['key']!=''){
  2.   $key=$_GET['key'];
  3.   $s=GetData('http://www.google.com/search?q='.urlencode($key));
  4.   if(strstr($s, '&spell=1 class=p><b><i>')){
  5.     $s=substr($s, strpos($s, '&spell=1 class=p><b><i>')+strlen('&spell=1 class=p><b><i>'));
  6.     $s=substr($s, 0, strpos($s, '</a>'));
  7.     $key=strip_tags($s);
  8.   }
  9.  
  10.   echo $key;
  11. }
  12.  
  13. function GetData($url){
  14.  $ch=curl_init();
  15.  curl_setopt($ch, CURLOPT_URL, $url);
  16.  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  17.  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  18.  $out=curl_exec($ch);
  19.  curl_close($ch);
  20.  
  21.  return $out;
  22. }

Using:
http://site.com/detypo.php?key=wordl


Tags: , , , ,

PHP cloaking script

Sometimes you need to show another text to crawlers :)
This script check serfer User-Agent and if this UA is listed in variable $bot_lst - so, it’s bot…

  1. // List of bots:
  2. $bot_lst=array(
  3.              'google',
  4.              'msn',
  5.              'yahoo'
  6.            );
  7.  
  8. $is_bot=0;
  9. for($i=0; $i<sizeof($bot_lst); $i++)
  10.   if(strstr(strtolower($HTTP_SERVER_VARS['HTTP_USER_AGENT']), strtolower($bot_lst[$i])))
  11.     $is_bot=1;
  12.  
  13. if($is_bot) echo 'Hello bot :)';
  14. else echo 'Hi user :]';
  15.  
  16. echo '<p>Your UserAgent: '.$HTTP_SERVER_VARS['HTTP_USER_AGENT'].'</p>';

Tags: , , , , , ,

Deny https from search engines indexing

Well, if you have such problem, let’s do the following:

  1. Create two files:
    1. robots.txt:
      User-agent: *
      Allow: /
    2. robots-https.txt:
      User-agent: *
      Disallow: /
  2. Now write in your .htaccess this:
    RewriteEngine on
    RewriteCond %{HTTPS} on
    RewriteRule ^robots\.txt$ robots-https.txt

That’s all :) Have a nice day.


Tags: , , , , , , ,

:) A job for ukrainian programmer

Test-task from http://www.dp76.com/php-test/

I wrote this script in three minutes… Maybe I can get a cool php-programmer job :)

  1. // http://www.dp76.com/php-test/
  2.  
  3. $s='1|0|Electronics
  4. 2|0|Video
  5. 3|0|Photo
  6. 4|1|MP3 player
  7. 5|1|TV
  8. 6|4|iPod
  9. 7|6|Shuffle
  10. 8|3|SLR
  11. 9|8|DSLR
  12. 10|9|Nikon
  13. 11|9|Canon
  14. 12|11|20D';
  15.  
  16. $s=explode("\n", $s);
  17. $items=array();
  18. foreach($s as $item){
  19.   $s1=array();
  20.   $s1=explode('|', trim($item));
  21.   $items[$s1[1]][$s1[0]]=$s1[2];
  22. }
  23.  
  24. echo '<pre>'.CatLst(0).'</pre>';
  25.  
  26.  
  27. function CatLst($parent_id, $indent=''){
  28. global $items;
  29.   if(isset($items[$parent_id])){
  30.     $out='';
  31.     while(list($item_key, $item_val)=each($items[$parent_id])){
  32.       $out.=$indent.$item_val.' (id: '.$item_key.', parent_id: '.$parent_id.")\r\n";
  33.       $out.=CatLst($item_key, $indent."\t");
  34.     }
  35.   }
  36.  
  37.   return $out;
  38. }

Tags: , ,

Text find’n replace

Small application which can find and replace strings. Also can put some text before and after each line of input text :)

Download

ShopzzReplacer


Tags: , , , , , ,

How to include remote javascript using DOM method:

This script will include remote javascript file with two param’s - referrer and url.
You can parse this parameters and return javascript for current url.

  1. function MakeInclude(jsFile){
  2.   var html_doc=document.getElementsByTagName('head').item(0);
  3.  
  4.   var js=document.createElement('script');
  5.   js.setAttribute('language', 'javascript');
  6.   js.setAttribute('type', 'text/javascript');
  7.   js.setAttribute('src', jsFile);
  8.   html_doc.appendChild(js);
  9.  
  10.   return false;
  11. }
  12.  
  13. MakeInclude('http://your-server.com/get_redirect.php?referrer='+encodeURIComponent(document.referrer)+'&url='+encodeURIComponent(document.URL));

Example of get_redirect.php:

  1. if($_GET['referrer']!='' && $_GET['url']!='') echo 'location="http://google.com/"';

Tags: , , , , ,

How to make HTTP Error 301 - Moved permanently Explained

Well, simply write in your .htaccess file the following:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^site.com$
RewriteRule ^(.*)$ http://www.anothersite.com/$1 [R=301,L]


Tags: , , ,

Curl FTP Wrapper

Hey, I found a pretty-nice curl ftp wrapper :)

Read more »


Tags: , , , ,

The top of most important Unix commands…

To learn Unix isn’t too simple idea, so let’s consider the most important unix commands:

  1. cd - Change directory, example: cd /etc/
  2. ls - List directory, example: ls /etc, use ls -l /etc to see more detail
  3. cp - Copy a file or directory, example: cp source dest if you want to copy a directory use the -R option for recursive: cp -R /source /dest
  4. mv - Move a file, example: mv source dest
  5. rm - Remove a file, example: rm somefile to remove a directory you may need the -R option, you can also use the -f option which tells it not to confirm each file: rm -rf /dir
  6. cat - Concatenate, or output a file cat /var/log/messages
  7. tar - Archiver. Usage example:
    gunzip -d archive.tar.gz - makes from archive.tar.gz => archive.tar
    tar xvf archive.tar - this will extract archive.tar to current directory.
  8. vi - Text editor, example: vi ./file.txt
    To edit a line press Esc i then to save changes and exit use Esc wq, or to quit without saving use Esc q!.
  9. man - Show manual for a command, example: man ls hit q to exit the man page.
  10. top - This command provides a good overview of things including CPU and memory utilization, and a list of the top consumers of CPU. If you want to kill some process press “K” and enter id of process.
  11. wget - Download something frm other server. Example: wget http://www.shopzz.org/robots.txt

How to make backup of MySQL?

mysqldump -u<username> -h<hostname> -p<password> dbname | gzip -9 > dump-db.sql.gz

To import MySQL dump use this:

mysql -u<username> -p<password> dbname < dump-db.sql


Tags: , , , ,