دنبالر در حالت آرشیو است. امکان ورود یا ارسال وجود ندارد. برای دریافت پشتیبان به backup.donbaler.net مراجعه کنید. تماس: [email protected]

15 کد کاربردی در زبان php

انجمن ها » برنامه نویسی » زبان php

بهترین جواب انتخاب نشده یا پرسشی نیست
تاپیک با شناسه 180، حاوی 2 ارسال
رشته: رياضي و فيزيك
شغل: دانش اموز
امتیاز کاربری: 2975
#511
1.ارسال ایمیل HTML
$to = "[email protected]";
$subject = "VIRALPATEL.net";
$body = "Body of your message here you can use HTML too. e.g.   Bold ";
$headers = "From: Peter\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "Return-Path: [email protected]\r\n";
$headers .= "X-Mailer: PHP5\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to,$subject,$body,$headers);
2.فانکشن اینکد و دیکد Base64
function base64url_encode($plainText) {
	$base64 = base64_encode($plainText);
	$base64url = strtr($base64, '+/=', '-_,');
	return $base64url;
}

function base64url_decode($plainText) {
	$base64url = strtr($plainText, '-_,', '+/=');
	$base64 = base64_decode($base64url);
	return $base64;
} 
3.دریافت آی پی سیستم
function getRemoteIPAddress() {
	$ip = $_SERVER['REMOTE_ADDR'];
	return $ip;
}
اگر خواستید ای پی واقعی کاربر را هنگام اتصال به پروکسی دریافت کنید از کد زیر استفاده کنید.
function getRealIPAddr()
{
	if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
	{
		$ip=$_SERVER['HTTP_CLIENT_IP'];
	}
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
	{
		$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	else
	{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}
4.تبدیل ثانیه ها به روز , ساعت , دقیقه و ثانیه
function secsToStr($secs) {
	if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours1){$r.='s';}if($secs>0){$r.=', ';}}
	if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes1){$r.='s';}if($secs>0){$r.=', ';}}
	$r.=$secs.' second';if($secs1){$r.='s';}
	return $r;
}
5.بررسی فرمت آدرس ایمیل
$email = $_POST['email'];
if(preg_match("~([a-zA-Z0-9!#$%&'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).([a-zA-Z0-9]{2,4})~",$email)) {
	echo 'This is a valid email.';
} else{
	echo 'This is an invalid email.';
} 
ارسال شده در 1392/04/03، ساعت 16:52 بالا
رشته: رياضي و فيزيك
شغل: دانش اموز
امتیاز کاربری: 2975
#512
6.تولید خروجی XML با PHP
//this is a sample xml string
$xml_string="

    
        ben
        A
    
    
        h2o
        K
    
";

//load the xml string using simplexml function
$xml = simplexml_load_string($xml_string);

//loop through the each node of molecule
foreach ($xml->molecule as $record)
{
   //attribute are accessted by
   echo $record['name'], '  ';
   //node are accessted by -> operator
   echo $record->symbol, '  ';
   echo $record->code, '';
}
7.اتصال به دیتابیس
if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();
$dbHost = "localhost";        //Location Of Database usually its localhost
$dbUser = "xxxx";            //Database User Name
$dbPass = "xxxx";            //Database Password
$dbDatabase = "xxxx";       //Database Name

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");
mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

# This function will send an imitation 404 page if the user
# types in this files filename into the address bar.
# only files connecting with in the same directory as this
# file will be able to use it as well.
function send_404()
{
    header('HTTP/1.x 404 Not Found');
    print ''."n".
    ''."n".
    '404 Not Found'."n".
    ''."n".
    'Not Found'."n".
    'The requested URL '.
    str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
    ' was not found on this server.'."n".
    ''."n";
    exit;
}

# In any file you want to connect to the database, 
# and in this case we will name this file db.php 
# just add this line of php code (without the pound sign):
# include"db.php";
8.تولید و خواندن JSON - تولید
$json_data = array ('id'=>1,'name'=>"rolf",'country'=>'russia',"office"=>array("google","oracle"));
echo json_encode($json_data);
- خواندن
$json_string='{"id":1,"name":"rolf","country":"russia","office":["google","oracle"]} ';
$obj=json_decode($json_string);
//print the parsed data
echo $obj->name; //displays rolf
echo $obj->office[0]; //displays google
9.MySQL Timestamp در ---> PHP
$query = "select UNIX_TIMESTAMP(date_field) as mydate from mytable where 1=1";
$records = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($records))
{
	echo $row;
} 
ارسال شده در 1392/04/03، ساعت 16:57 بالا
رجوع به :