martes, 15 de noviembre de 2016

fiel() file_get_contents fopen()

he first two, file and file_get_contents are very similar. They both read an entire file, but filereads the file into an array, while file_get_contents reads it into a string. The array returned by file will be separated by newline, but each element will still have the terminating newline attached, so you will still need to watch out for that.
The fopen function does something entirely different—it opens a file descriptor, which functions as a stream to read or write the file. It is a much lower-level function, a simple wrapper around the C fopen function, and simply calling fopen won't do anything but open a stream.
Once you've open a handle to the file, you can use other functions like fread and fwrite to manipulate the data the handle refers to, and once you're done, you will need to close the stream by using fclose. These give you much finer control over the file you are reading, and if you need raw binary data, you may need to use them, but usually you can stick with the higher-level functions.
So, to recap:
  • file — Reads entire file contents into an array of lines.
  • file_get_contents — Reads entire file contents into a string.
  • fopen — Opens a file handle that can be manipulated with other library functions, but does no reading or writing itself.

jueves, 10 de noviembre de 2016

iptables php

<?php

$remoteip=$argv[1];
system("iptables -A INPUT -s $remoteip -j DROP");

?>

php block.php 100.100.2.2



verify
 iptables -L -n

IP monitor

<?php

$sip_peers=shell_exec("/usr/sbin/asterisk -x \" sip show peers\" | awk '{print $2}' | grep -v Unspecified | grep -v sip | grep -v Host");

$ip=explode("\n",$sip_peers);

foreach($ip as $key=>$value){

$url=file_get_contents("http://ipinfo.io/$value");

$url=json_decode($url, true);

if($url['country']!="US") {


echo  $url['country']." RED ALERT  UNKNOWN IP ". $url['ip']."\n";

$body="Unauthorized peer   with the IP ". $url['ip'];

$headers = 'From: Asterisk Dominicana <ambiorixg12@asterisk-dominicana.com>' . "\r\n";

$headers  .= 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail("ambiorixg12@gmail.com"," Asterisk  Alert",$body,$headers);

$remoteip=$url['ip'];
system("/sbin/iptables -A INPUT -s $remoteip -j DROP");

continue;
}
echo $url['ip']." ".$url['country']."\n";

}
?>


*/2 * * * * /usr/bin/php /root/ip_monitor.php >> /root/ip-results.txt 2>> /root/ip-errror.txt

empty() / isset() is_null comparison.

Comparisons of $x with PHP functions
Expressiongettype()empty()is_null()isset()boolean : if($x)
$x = "";stringTRUEFALSETRUEFALSE
$x = null;NULLTRUETRUEFALSEFALSE
var $x;NULLTRUETRUEFALSEFALSE
$x is undefinedNULLTRUETRUEFALSEFALSE
$x = array();arrayTRUEFALSETRUEFALSE
$x = array('a', 'b');arrayFALSEFALSETRUETRUE
$x = false;booleanTRUEFALSETRUEFALSE
$x = true;booleanFALSEFALSETRUETRUE
$x = 1;integerFALSEFALSETRUETRUE
$x = 42;integerFALSEFALSETRUETRUE
$x = 0;integerTRUEFALSETRUEFALSE
$x = -1;integerFALSEFALSETRUETRUE
$x = "1";stringFALSEFALSETRUETRUE
$x = "0";stringTRUEFALSETRUEFALSE
$x = "-1";stringFALSEFALSETRUETRUE
$x = "php";stringFALSEFALSETRUETRUE
$x = "true";stringFALSEFALSETRUETRUE
$x = "false";stringFALSEFALSETRUETRUE



OTHER TOPIC
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

A simple empty() / isset() comparison.
<?php
$var 
0;// Evaluates to true because $var is emptyif (empty($var)) {
    echo 
'$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is setif (isset($var)) {
    echo 
'$var is set even though it is empty';
}
?>

viernes, 4 de noviembre de 2016

php cookies

Setting new cookie
=============================
<?php
setcookie
("name","value",time()+$int);/*name is your cookie's name
value is cookie's value
$int is time of cookie expires*/
?>
Getting Cookie
=============================
<?php echo $_COOKIE["your cookie name"];?>
Updating Cookie
=============================
<?php
setcookie
("color","red");
echo 
$_COOKIE["color"];/*color is red*/
/* your codes and functions*/
setcookie("color","blue");
echo 
$_COOKIE["color"];/*new color is blue*/?>
Deleting Cookie
==============================
<?php unset($_COOKIE["yourcookie"]);/*Or*/setcookie("yourcookie","yourvalue",time()-1);/*it expired so it's deleted*/?>
Reference: http://gencbilgin.net/php-cookie-kullanimi.html

replacing values

<?php
$bodytag 
str_ireplace("%body%""black""<body text=%BODY%>");
echo 
$bodytag// <body text=black>?>

jueves, 3 de noviembre de 2016

Example #3 Uploading array of files


PHP supports HTML array feature even with files.
<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>
<?phpforeach ($_FILES["pictures"]["error"] as $key => $error) {
    if (
$error == UPLOAD_ERR_OK) {
        
$tmp_name $_FILES["pictures"]["tmp_name"][$key];
        
// basename() may prevent filesystem traversal attacks;
        // further validation/sanitation of the filename may be appropriate
        
$name basename($_FILES["pictures"]["name"][$key]);
        
move_uploaded_file($tmp_name"data/$name");
    }
}
?>