viernes, 5 de febrero de 2016

allowing IP from specific country

<?php
function filter($IP) {

$remote=popen(" curl  -Ss ipinfo.io/$IP  | awk '{print $2}'  | awk 'NR==6'");
if($remote==USA){

echo " Welcome";
}


else  {

echo "You cant access to this website";

exit();

}

}

filter($_SERVER['REMOTE_ADDR']);
?>



$country = file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country");
if($country != "RO") {
    // User isn't in Romania! 
    // Display an error message, or redirect to another website
}

http://stackoverflow.com/questions/8384421/restrict-website-to-access-specific-country


http://ipinfo.io/213.140.32.1/country

note if you use system() function instead of popen() the command output will be displayed on the browser. this is useful  for debugging popurses

domingo, 31 de enero de 2016

Export MySQL table to CSV using PHP


Lets write a simple php program to export data from MySql table to CSV file. 

PHP Code

<?php

// Database Connection

$host="localhost";
$uname="root";
$pass="";
$database = "a2zwebhelp"; 

$connection=mysql_connect($host,$uname,$pass); 

echo mysql_error();

//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or 
die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected <br>");

// Fetch Record from Database

$output = "";
$table = ""; // Enter Your Table Name 
$sql = mysql_query("select * from $table");
$columns_total = mysql_num_fields($sql);

// Get The Field Name

for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}

// Download the file

$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;

?>
http://www.a2zwebhelp.com/export-data-to-csv

sábado, 30 de enero de 2016

using default values on function Passing arguments by reference and return values

<?php
function alert($type=5) {

if ($type==1){

return "num is equal to 1  ";

}


else  {


return  " Bigger than 1";


}
}

echo alert(1);


?>


Passing arguments by reference ¶




To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition:
<?php
function alert(&$type=5) {


$type +=6;
if ($type==1){


return "type is equal to 1  ";

}


else  {


return  " $type Bigger than 1";


}
}

echo alert();

jueves, 21 de enero de 2016

exporting to csv

<?php

$list = array (
    array('Name', 'LastName', 'Phone', 'State'),
    array('Chris', 'Lopez', '789-344-4543','NY'),

);

$fp = fopen('/var/www/csv/file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
echo  "Ok";
?>


chmod  -R 777 /var/www/csv/file.csv

viernes, 8 de enero de 2016

IP validator

<?php

/* see current location
echo getcwd();
*/
$hacker_IP=$_SERVER['REMOTE_ADDR'];

$auth_IP = file_get_contents('http://65.181.118.232/ip.php');

$allowed_IP=array($auth_IP);


function  username_check($hacker_IP,$allowed_IP) {


if(!in_array($hacker_IP,$allowed_IP)){

$body="Unauthorized login attempt   with the IP $hacker_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@hotmail.com"," Joomla Alert",$body,$headers);

echo "I'm Sorry $hacker_IP  you are not allowed to be here";
exit();

}


}


username_check($hacker_IP,$allowed_IP);
?>

miércoles, 30 de diciembre de 2015

multi dimensional array

<?php

$multi=array("a"=>1,"b"=>2,"c"=>array("d"=>array("e"=>500)));

echo $multi['c']['d']['e'];

echo "<br>";

foreach( $multi['c']['d'] as $key=> $value){

echo " Key is $key  Value is $value <br>";

}

?>

Accessing array elements

Example #6 Accessing array elements
<?php
$array 
= array(
    
"foo" => "bar",
    
42    => 24,
    
"multi" => array(
         
"dimensional" => array(
             
"array" => "foo"
         
)
    )
);
var_dump($array["foo"]);var_dump($array[42]);var_dump($array["multi"]["dimensional"]["array"]);?>


string(3) "bar"
int(24)
string(3) "foo"