پاسخ : مجموعه کدهای بدرد بخور در php
Whois با PHP :
کد PHP:
<?php
function whois_query($domain) {
// fix the domain name:
$domain = strtolower(trim($domain));
$domain = preg_replace('#^http:\/\/#i', '', $domain);
$domain = preg_replace('#^www\.#i', '', $domain);
$domain = explode('/', $domain);
$domain = trim($domain[0]);
// split the TLD from domain name
$_domain = explode('.', $domain);
$lst = count($_domain)-1;
$ext = $_domain[$lst];
// You find resources and lists
// like these on wikipedia:
//
// http://de.wikipedia.org/wiki/Whois
//
$servers = array(
'ac' => 'whois.nic.ac',
'ae' => 'whois.uaenic.ae',
'aero' => 'whois.information.aero',
'at' => 'whois.ripe.net',
'au' => 'whois.aunic.net',
'be' => 'whois.dns.be',
'bg' => 'whois.ripe.net',
'biz' => 'whois.neulevel.biz',
'br' => 'whois.registro.br',
'bz' => 'whois.belizenic.bz',
'ca' => 'whois.cira.ca',
'cc' => 'whois.nic.cc',
'ch' => 'whois.nic.ch',
'cl' => 'whois.nic.cl',
'cn' => 'whois.cnnic.net.cn',
'com' => 'whois.internic.net',
'coop' => 'whois.nic.coop',
'cz' => 'whois.nic.cz',
'de' => 'whois.nic.de',
'edu' => 'whois.internic.net',
'fr' => 'whois.nic.fr',
'gov' => 'whois.nic.gov',
'hu' => 'whois.nic.hu',
'ie' => 'whois.domainregistry.ie',
'il' => 'whois.isoc.org.il',
'in' => 'whois.ncst.ernet.in',
'info' => 'whois.nic.info',
'int' => 'whois.iana.org',
'ir' => 'whois.nic.ir',
'mc' => 'whois.ripe.net',
'mil' => 'rs.internic.net',
'name' => 'whois.nic.name',
'net' => 'whois.internic.net',
'nl' => 'whois.domain-registry.nl'
'org' => 'whois.pir.org',
'ru' => 'whois.ripn.net',
'to' => 'whois.tonic.to',
'tv' => 'whois.tv',
'us' => 'whois.nic.us',
);
if (!isset($servers[$ext]) || !in_array($ext, $servers)) {
die('Error: No matching nic server found!');
}
$nic_server = $servers[$ext];
$output = '';
// connect to whois server:
if ($conn = fsockopen ($nic_server, 43)) {
fputs($conn, $domain."\r\n");
while(!feof($conn)) {
$output .= fgets($conn, 128);
}
fclose($conn);
}
else {
die('Error: Could not connect to ' . $nic_server . '!');
}
return $output;
}
?>
پاسخ : مجموعه کدهای بدرد بخور در php
فرستادن خطاهای PHP به ایمیل شما بجای نمایش در صفحه:
کد PHP:
<?php
// Our custom error handler
function mail_error_handler($number, $message, $file, $line, $vars) {
$email = "
<p>An error ({$number}) occurred on line
<strong>{$line}</strong> and in the <strong>file: {$file}.</strong>
<p> {$message} </p>";
$email .= "<pre>" . print_r($vars, 1) . "</pre>";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Email the error to someone...
@mail($email, 'PHP_ERROR', 'you@youremail.com', $headers);
// Make sure that you decide how to respond to errors (on the user's side)
// Either echo an error message, or kill the entire project. Up to you...
// The code below ensures that we only "die" if the error was more than
// just a NOTICE.
if (($number !== E_NOTICE) && ($number < 2048)) {
die('There was an error. Please try again later.');
}
}
// We should use our custom function to handle errors.
set_error_handler('mail_error_handler');
// Trigger an error... (var doesn't exist)
echo $somevarthatdoesnotexist;
?>
پاسخ : مجموعه کدهای بدرد بخور در php
آپلود فایل در FTP :
کد PHP:
<?php
// FTP access parameters
$host = 'ftp.example.org';
$usr = 'example_user';
$pwd = 'example_password';
// file to move:
$local_file = './example.txt';
$ftp_path = '/data/example.txt';
// connect to FTP server (port 21)
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");
// send access parameters
ftp_login($conn_id, $usr, $pwd) or die("Cannot login");
// turn on passive mode transfers (some servers need this)
// ftp_pasv ($conn_id, true);
// perform file upload
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII);
// check upload status:
print (!$upload) ? 'Cannot upload' : 'Upload complete';
print "\n";
/*
** Chmod the file (just as example)
*/
// If you are using PHP4 then you need to use this code:
// (because the "ftp_chmod" command is just available in PHP5+)
if (!function_exists('ftp_chmod')) {
function ftp_chmod($ftp_stream, $mode, $filename){
return ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
}
}
// try to chmod the new file to 666 (writeable)
if (ftp_chmod($conn_id, 0666, $ftp_path) !== false) {
print $ftp_path . " chmoded successfully to 666\n";
} else {
print "could not chmod $file\n";
}
// close the FTP stream
ftp_close($conn_id);
?>
پاسخ : مجموعه کدهای بدرد بخور در php
جداسازی اعداد از رشته :
کد PHP:
<?php
// extract numbers from a string
// http://php.snippetdb.com
$string = "The 1. Quick and 2. Brown fox said 3. (!@*(#!@*";
$new_string = ereg_replace("[^0-9]", "", $string);
echo $new_string;
?>
پاسخ : مجموعه کدهای بدرد بخور در php
یک فانکشن خیلی خیلی باحال ! تولید متن رنگارنگ (منظور موج رنگها) :
کد PHP:
<?
//simple to use!
echo fadeText('ff0000','0000ff','this is the text that will be faded!');
function getColor($startc, $endc, $percentc)
{
$r1=hexdec(substr($startc,-6,2));
$g1=hexdec(substr($startc,-4,2));
$b1=hexdec(substr($startc,-2,2));
$r2=hexdec(substr($endc,-6,2));
$g2=hexdec(substr($endc,-4,2));
$b2=hexdec(substr($endc,-2,2));
$pcc = $percentc/100;
$rcc= ($r1+($pcc*($r2-$r1)))*1;
$gcc= ($g1+($pcc*($g2-$g1)))*1;
$bcc= ($b1+($pcc*($b2-$b1)))*1;
$frc= dechex($rcc);
$fgc= dechex($gcc);
$fbc= dechex($bcc);
if (strlen($frc)=="1"){
$frc="0".$frc;
}
if (strlen($fgc)=="1"){
$fgc="0".$fgc;
}
if (strlen($fbc)=="1"){
$fbc="0".$fbc;
}
$dasclr="#".$frc."".$fgc."".$fbc;
return $dasclr;
}
function fadeText($start, $end, $msg)
{
$myString2Fade = $msg;
$endResult="";
$daslen=strlen($myString2Fade);
for ($i=0 ; $i<$daslen ; $i++){
$perc=(100/$daslen)*$i;
if ($myString2Fade[$i]==" "){
$endResult="$endResult ";
} else {
$endResult="$endResult<font color='".getColor($start, $end, $perc)."'>".$myString2Fade[$i]."</font>";
}
}
return $endResult;
}
?>
پاسخ : مجموعه کدهای بدرد بخور در php
یک مثال برای DOM :
کد PHP:
<?php
// example HTML code: (could also come from an URL)
$html = '<html>
<head>
<title>links</title>
</head>
<body>
<a href="link1.htm" title="Link title 1" target="_blank">Link #1</a>
<a href="link2.htm" title="Link title 2" target="_blank">Link #2</a>
<a href="link3.htm" title="Link title 3" target="_blank">Link #3</a>
</body>
</html>';
// check if DomXML is available:
if (!function_exists('DomDocument')){
die('DomXML extension is not available :-(');
}
print '<pre>';
// create new DOM object:
$dom = new DomDocument();
// load HTML code:
$dom->loadHtml($html);
// get tags by tagname (all <a> tags / links):
$tags = $dom->getElementsByTagName('a');
// loop trough all links:
foreach ($tags as $a){
print '<b>' . $a->nodeValue . '</b>';
// does this tag have attributes:
if ($a->hasAttributes()){
// loop trough all attributes:
foreach ($a->attributes as $attribute){
print '- ' . $attribute->name . ': ' . $attribute->value;
print "";
}
}
print "<hr/>";
}
print '</pre>';
?>
پاسخ : مجموعه کدهای بدرد بخور در php
یک مثال ساده برای تولید پیج داینامیک بوسیله لینکها و Mysql :
کد PHP:
<!---------------------------------- Change File name To links.php -------------------->
<!-- links.php -->
<a href="links.php?id=1">File 1</a>
<a href="links.php?id=2">File 2</a>
<a href="links.php?id=3">File 3</a>
<?php
// Load the database connections
include 'db.php';
//check to see if one of the links were clicked
echo '<div>';
if(isset($_GET['id'])){
// Make a safe string
$id = mysql_real_escape_string($_GET['id']);
// Query the database
$sql = mysql_query("SELECT * FROM table_name WHERE id='$id'");
if(mysql_num_rows($sql) == 1){
// If one row was found, create an array of the row
$row = mysql_fetch_array($sql);
// echo the column 'content' (change 'content' to your column name)
echo '<p>'.$row['content'].'</p>';
}else{
// Row was not found
echo '<p>File Not Found.</p>';
}
}else{
// the id parameter in the url was not set
echo '<p>Invalid URL.</p>';
}
echo '</div>';
?>
پاسخ : مجموعه کدهای بدرد بخور در php
نمایش بخشی از تصویر
کد PHP:
<?php
header('Content-type: image/jpeg');
if(!isset($_GET['file']) || !file_exists($_GET['file']) || strtolower(substr($_GET['file'], strrpos($_GET['file'], '.'))) != '.jpg') {
$im = imagecreatetruecolor(100, 100);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 50, 50, $white);
imagestring($im, 5, 30, 40, 'Error', $black);
imagejpeg($im);
imagedestroy($im);
exit();
}
$src = imagecreatefromjpeg($_GET['file']);
$sw = imagesx($src);
$sh = imagesy($src);
$x1 = (isset($_GET['x1']) && is_numeric($_GET['x1']) && $_GET['x1'] >= 0 && $_GET['x1'] < $sw) ? (int) $_GET['x1'] : 0;
$y1 = (isset($_GET['y1']) && is_numeric($_GET['y1']) && $_GET['y1'] >= 0 && $_GET['y1'] < $sh) ? (int) $_GET['y1'] : 0;
$x2 = (isset($_GET['x2']) && is_numeric($_GET['x2']) && $_GET['x2'] >= 0 && $_GET['x2'] < $sw) ? (int) $_GET['x2'] : $sw;
$y2 = (isset($_GET['y2']) && is_numeric($_GET['y2']) && $_GET['y2'] >= 0 && $_GET['y2'] < $sh) ? (int) $_GET['y2'] : $sh;
$tl = array('x' => min ($x1, $x2), 'y' => min($y1, $y2));
$br = array('x' => max ($x1, $x2), 'y' => max($y1, $y2));
$dw = abs($x1 - $x2);
$dh = abs($y1 - $y2);
$dst = imagecreatetruecolor($dw, $dh);
imagecopyresized($dst, $src, 0, 0, $tl['x'], $tl['y'], $dw, $dh, $br['x'], $br['y']);
imagejpeg($dst);
imagedestroy($dst);
imagedestroy($src);
?>
نحوه استفاده :
کد PHP:
<img src="pim.php?file=image.jpg&x1=50&y1=50&x2=200&y2=100"/>
(با فرض اینکه کد فوق رو به اسم pim.php ذخیره کرده باشین).
کاربرد اصلی این اسکریپت برای وقتی هست که میخواین یک تصویر رو بصورت تکه تکه نشون بدین و با CSS تنظیم کنید تا کنار هم ظاهر بشه. دیگه نیازی نیست توی Photoshop یا سایر برنامه ها تصویر رو برش بدین و توی فایلهای جداگانه ذخیره کنید و در فضای هاست هم صرفه جویی میشه چون برای هر فایل جداگانه، هدرهای تصویر ذخیره میشن و این یعنی تکرار هدرها. کد فوق رو با کمی تغییر میتونید برای سایر انواع تصویر مثل png و... هم استفاده کنید.
موفق باشید.
پاسخ : مجموعه کدهای بدرد بخور در php
Backup گرفتن از دیتابیس
کد PHP:
<?php
function backup_db($host, $user, $pass, $name, $tables = '*') {
date_default_timezone_set('Asia/Tehran');
$return = '';
mysql_connect($host,$user,$pass) or die('Connection error');
mysql_select_db($name) or die('Database error');
mysql_query('SET NAMES \'utf8\'');
mysql_set_charset('utf8');
if($tables == '*') {
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
mysql_free_result($result);
}
else {
$tables = is_array($tables) ? $tables : explode(',', $tables);
}
foreach($tables as $table) {
$result = mysql_query('SELECT * FROM `'.$table.'`');
$num_fields = mysql_num_fields($result);
$return .= 'DROP TABLE IF EXISTS `'.$table.'`;'.PHP_EOL.PHP_EOL;
$row = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$table.'`'));
$return .= $row[1].';'.PHP_EOL.PHP_EOL;
for ($i = 0; $i < $num_fields; $i++) {
while($row = mysql_fetch_row($result)) {
$return.= 'INSERT INTO `'.$table.'` VALUES(';
for($j = 0; $j < $num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace('\n', '\\n', $row[$j]);
if (isset($row[$j])) {
$return .= '\''.$row[$j].'\'';
}
else {
$return .= '\'\'';
}
if ($j < ($num_fields - 1)) {
$return .= ',';
}
}
$return .= ');'.PHP_EOL;
}
}
$return .= PHP_EOL.PHP_EOL.PHP_EOL;
}
$handle = fopen('db-backup-'.$name.'-'.date('Y,m,d-H,i,s').'.sql', 'w');
fwrite($handle, $return);
fclose($handle);
}
?>
مثالی از نحوه استفاده از تابع بالا :
کد PHP:
backup_db('localhost', 'root', '', 'your_db_name');
پاسخ : مجموعه کدهای بدرد بخور در php
جداسازی آدرس ها از متن و تبدیل به لینک :
کد PHP:
<?php
function to_link($string){
return preg_replace("~(http|https|ftp|ftps)://(.*?)(\s|\n|[,.?!](\s|\n)|$)~", '<a href="$1://$2">$1://$2</a>$3',$string);
}
$html = 'This line of text has three urls: http://webhostingtalk.ir http://yahoo.com and http://google.com';
echo to_link($html, TRUE);
?>