Mövzu - Developer-lar ucun faydali php kodlar.

Developer-lar ucun faydali php kodlar.



Alt bölmə: PHP
Fəsil: Proqramlaşdırma

Tema faylları
Şərhlər 15
ugurlar
24 Yan 2022, 09:30
0
Təşəkkürlər əla paylaşımlardı
22 Yan 2022, 20:15
0
[c]# V3N0M ([time]21.01.2022 16:09[/time])
GameKing, php dan başqa js kodlarda paylaşa bilərsiniz başqa mövzuda? Bildiyim qədəri ilə java biliyinizdə yaxşı səviyyədədi :)[/c]Nezere alindi, imkan etsem ayri movzuda paylasaram.
21 Yan 2022, 20:14
0
GameKing, php dan başqa js kodlarda paylaşa bilərsiniz başqa mövzuda? Bildiyim qədəri ilə java biliyinizdə yaxşı səviyyədədi :)
21 Yan 2022, 20:09
0
#11, #12 tesekkuler
----
31.Sehifenin nece saniye erzinde yuklendiyini gosteren kod.

[code=php]
$time_start = microtime(true);
// Place your s here...
echo 'Total execution time in seconds: ' . (microtime(true) - $time_start);
[/code]
----
32.Asagidaki kod web sehifesinin tam URL adresini elde edecek. (eger ki SSL istifade olunursa, HTTP-ni HTTPS-e cevir)

[code=php]
$url = ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
[/code]
----
33.Bir reqemi formaya sal.

[code=php]
$num = number_format(1000);
echo $num; // 1,000
[/code]

#################################################################

Float tipinde bir reqemi formaya sal.

[code=php]
$num = number_format(1000.12, 2);
echo $num; // 1,000.12
[/code]
----
34.Bir zip fayli arxivden ac.

[code=php]
$zip = new ZipArchive;
$file = $zip->open('file.zip');
if ($file) {
$zip->extractTo('/extract_path/');
$zip->close();
echo 'Archive extracted successfully!';
}
[/code]
----
35.Mobile detection php kodu.

[code=php]
if (preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up.browser|up.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])) {
// Is mobile...
}
[/code]
----
36.Bir faylin movcud olub-olmadigini yoxla.

[code=php]
if (file_exists('example.png')) {
// File exists
}
[/code]
21 Yan 2022, 19:18
0
Ugurlar olsun bro
21 Yan 2022, 13:36
0
Mövzu Sahibinin İstəyinə Uyğun Artıq Şərhlər Silindi
21 Yan 2022, 09:50
0
[c]# Hesenov ([time]19.01.2022 19:29[/time])
GameKing, Məntiqi Qura bilməyən özünə developer deməsində mən sadəcə deyirəmki yəni mövzu başlığı Uyğun gəlmir dostum Paylaşdığın kodlar yararlıdı amaki day bunları bilməyən biridə özünə developer de[/c]Dediyim kimi movzunu komek xarakterli acmisam isteyen kodlari goturub oldugu kimi istifade eder isteyen ise ona benzer kod yazar.

Elave olaraq birseyi de qeyd edim ki, mentiq de oz daxilinde iki yere ayrilir (cetin ve asan) ele mentiqi kodlar var ki asan olur hansi ki qurulmasi bir nece deqiqe vaxtini alir lakin elesi de var ki saatlarca oturub fikirlesmeli olursan hemin cetin aliqrotmani qura bilmeyende ise mecburi google'dan komek almali olursan.

Dediyim kimi her developer herseyi 100 de 100 bilmir onun da mutleq bilmediyi seyler var ve buna gore hemin eksiklikleri ozunde tamamlamalidir.

Mene gore eger ki kodu oxuyub anlaya bilirsense o sexs developer sayilir.
----
21.URL-de teyin olunan GET sorgusunu elde etmek.

[code=php]
// index.php?name=david
echo $_GET['name'];
// index.php?name=david&surname=adams
echo $_GET['surname'];
[/code]

###############################################################

GET sorgusunu URL-de var olub olmadigini yoxlamaq.

[code=php]
if (isset($_GET['name'])) {
// exists
}
[/code]

###############################################################

PHP ile form data prossesi.

[code=html]
<form action="login.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit">
</form>
[/code]

login.php

[code=php]
$user = $_POST['username'];
$pass = $_POST['password'];
echo 'Your username is ' . $user . ' and your password is ' . $pass . '.';
[/code]
----
22.One-way hashing alqoritmasindan istifade ederek, guvenli sekilde bir sifre yaratmaq.

[code=php]
$hash = password_hash('your_password', PASSWORD_DEFAULT);
[/code]

###############################################################

Yuxardaki metod ile yaradilan sifreni yoxlamaq.

[code=php]
if (password_verify('your_password', $hash)) {
// hash is valid
}
[/code]
20 Yan 2022, 16:55
0
GameKing, Məntiqi Qura bilməyən özünə developer deməsində mən sadəcə deyirəmki yəni mövzu başlığı Uyğun gəlmir dostum Paylaşdığın kodlar yararlıdı amaki day bunları bilməyən biridə özünə developer deməsində o ki birşeyin mənbəsi öz əlində deyilsə və həmin adam özünə developer deyirsə o ujey hazıra qaçmaqdı və developerlik deyil ümid edirəmki fikrimi çatdıra bildim
----
GameKing, Və Bu arada Mən sənə dmmirəmki mövzu adı dəyişdir deyədə bilmərəm çünki mövzu sənindir Sadəcə fikir bildirirəm
19 Yan 2022, 23:29
0
GameKing, Bizim forumlarda belə mövzu görülmədiyi üçün yadırğanır. :e1561: siz davam edin paylaşdıqlarınız işimə yaradı :)
19 Yan 2022, 23:04
0
Mövzu ADMIN tərəfindən PHP bölməsinə köçürüldü.
Mövzu açarkən diqqətli olun!
19 Yan 2022, 08:26
0
8.json data yarat ve ya parse et.

[code=php]
$json_data = array ('id'=>1,'name'=>"rolf",'country'=>'russia',"office"=>array("google","oracle"));
echo json_encode($json_data);
[/code]

[code=php]
$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
[/code]
----
9.mysql timestamp prosessi.

[code=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;
}
[/code]
----
10.random olaraq 25 simvol uzunlugunda string yarat.

[code=php]
<?php
# This particular code will generate a random string
# that is 25 charicters long 25 comes from the number
# that is in the for loop
$string = "abcdefghijklmnopqrstuvwxyz0123456789";
for($i=0;$i<25;$i++){
$pos = rand(0,36);
$str .= $string{$pos};
}
echo $str;
# If you have a database you can save the string in
# there, and send the user an email with the code in
# it they then can click a link or copy the code
# and you can then verify that that is the correct email
# or verify what ever you want to verify
?>
[/code]
----
11.YYYY-MM-DD formatindaki date validation php kodu.

[code=php]
function checkDateFormat($date)
{
//match the format of the date
if (preg_match ("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/", $date, $parts))
{
//check weather the date is valid of not
if(checkdate($parts[2],$parts[3],$parts[1]))
return true;
else
return false;
}
else
return false;
}
[/code]
----
12.http yonlendirme kodu.

[code=php]
<?php
header('Location: // stick your url here
?>
[/code]
----
13.qovluq icindeki fayllari goster.

[code=php]
<?php

function list_files($dir)
{
if(is_dir($dir))
{
if($handle = opendir($dir))
{
while(($file = readdir($handle)) !== false)
{
if($file != "." &amp;&amp; $file != ".." &amp;&amp; $file != "Thumbs.db"/*pesky windows, images..*/)
{
echo '<a target="_blank" href="'.$dir.$file.'">'.$file.'</a><br>'."n";
}
}
closedir($handle);
}
}
}

/*
To use:

<?php
list_files("images/");
?>
*/
?>
[/code]
----
14.browser detection php kodu.

[code=php]
<?php
$useragent = $_SERVER ['HTTP_USER_AGENT'];
echo "<b>Your User Agent is</b>: " . $useragent;
?>
[/code]
----
15.fayli zipden cixarmaq php kodu.

[code=php]
<?php
function unzip($location,$newLocation){
if(exec("unzip $location",$arr)){
mkdir($newLocation);
for($i = 1;$i< count($arr);$i++){
$file = trim(preg_replace("~inflating: ~","",$arr[$i]));
copy($location.'/'.$file,$newLocation.'/'.$file);
unlink($location.'/'.$file);
}
return TRUE;
}else{
return FALSE;
}
}
?>
//Use the code as following:
<?php
include 'functions.php';
if(unzip('zipedfiles/test.zip','unziped/myNewZip'))
echo 'Success!';
else
echo 'Error';
?>
[/code]
18 Yan 2022, 22:44
0
1.[mail] adli function'dan istifade ederek, php'de mail gonder.

[code=php]
$to = "pefake9109@huekieu.com";
$subject = "Subject_of_your_mail";
$body = "Body of your message here you can use HTML too. e.g. <br> <b> Bold </b>";
$headers = "From: Johnrn";
$headers .= "Reply-To: pefake9109@huekieu.comrn";
$headers .= "Return-Path: pefake9109@huekieu.comrn";
$headers .= "X-Mailer: PHP5n";
$headers .= 'MIME-Version: 1.0' . "n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
mail($to,$subject,$body,$headers);
?>
[/code]
----
2.string'in base64 encode & decode kodlasdirilmasi.

[code=php]
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;
}
[/code]
----
3.sayta daxil olan istifadecinin real ip adress'ini elde et.

[code=php]
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;
}
[/code]
----
4.saniye cinsinden parameter'e girilen deyeri qarsiligi olan gun, saat, deq ve saniye olan metne cevir.

[code=php]
function secsToStr($secs) {
if($secs>=86400){$days=floor($secs/86400);$secs=$secs%86400;$r=$days.' day';if($days<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=3600){$hours=floor($secs/3600);$secs=$secs%3600;$r.=$hours.' hour';if($hours<>1){$r.='s';}if($secs>0){$r.=', ';}}
if($secs>=60){$minutes=floor($secs/60);$secs=$secs%60;$r.=$minutes.' minute';if($minutes<>1){$r.='s';}if($secs>0){$r.=', ';}}
$r.=$secs.' second';if($secs<>1){$r.='s';}
return $r;
}
[/code]
----
5.email validation php kodu.

[code=php]
$email = $_POST['email'];
if(preg_match("~([a-zA-Z0-9!#$%&amp;'*+-/=?^_`{|}~])@([a-zA-Z0-9-]).([a-zA-Z0-9]{2,4})~",$email)) {
echo 'This is a valid email.';
} else{
echo 'This is an invalid email.';
}
[/code]
----
6.asan yol ile xml'i parse et.

[code=php]
//this is a sample xml string
$xml_string="<?xml version='1.0'?>
<moleculedb>
<molecule name='Benzine'>
<symbol>ben</symbol>
<code>A</code>
</molecule>
<molecule name='Water'>
<symbol>h2o</symbol>
<code>K</code>
</molecule>
</moleculedb>";

//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, '<br />';
}
[/code]
----
7.database connection php kodu.

[code=php]
<?php
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 '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'."n".
'<html><head>'."n".
'<title>404 Not Found</title>'."n".
'</head><body>'."n".
'<h1>Not Found</h1>'."n".
'<p>The requested URL '.
str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).
' was not found on this server.</p>'."n".
'</body></html>'."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";
?>
[/code]
18 Yan 2022, 22:13
0
Movzunun basligindan da bilindiyi kimi, bu movzunu php developer'lara komek olmaq meqsedi ile acmisam.

Hemin developer'lar ucun faydali ola bilecek php kodlari vaxt etdikce bu movzuda paylasacam.

Musbet serh yazan herkese tesekkurler.

-------------------------------------------------------------------------------------------------------------

As you know from the title of the topic, I opened this topic to help php developers.

I'll share php codes that can be useful for these developers on this topic as time goes on.

Thanks to everyone who wrote a positive comment.
18 Yan 2022, 21:01
0
Təbriklər ! Yeni mövzu uğurla yaradıldı! Əgər mövzu Qaydalara uyğun olmazsa silinəcək. Mövzudan kənara çıxmayın, mövzuda başqasının fikirlərinə hörmətlə yanaşın, probleminiz varsa şəxsidə həll edin, mövzuda artıq post yazmayın. Qeyd ! Ailəsinə hörmət edən, qeyrəti olan söyüş söyməz,forum reklam etməz
18 Yan 2022, 21:01
0
Bağışlayın, sizin üçün şərh yazmaq əlçatan deyil
Geriyə

azTOP.biz
Onlayn istifadəçilər
HAMISI