domingo, 28 de febrero de 2016

PHP curl

If you’re a Linux user then you’ve probably used cURL. It’s a powerful tool used for everything from sending email to downloading the latest My Little Pony subtitles. In this article I’ll explain how to use the cURL extension in PHP. The extension offers us the functionality as the console utility in the comfortable world of PHP. I’ll discuss sending GET and POST requests, handling login cookies, and FTP functionality.
Before we begin, make sure you have the extension (and the libcURL library) installed. It’s not installed by default. In most cases it can be installed using your system’s package manager, but barring that you can find instructions in the PHP manual.

How Does it Work?

All cURL requests follow the same basic pattern:
  1. First we initialize the cURL resource (often abbreviated as ch for “cURL handle”) by calling thecurl_init() function.
  2. Next we set various options, such as the URL, request method, payload data, etc. Options can be set individually with curl_setopt(), or we can pass an array of options to curl_setopt_array().
  3. Then we execute the request by calling curl_exec().
  4. Finally, we free the resource to clear out memory.

viernes, 19 de febrero de 2016

PHP Getting yesterday date



http://stackoverflow.com/questions/6796866/php-date-yesterday


130down voteaccepted
date() itself is only for formatting, but it accepts a second parameter.
date("F j, Y", time() - 60 * 60 * 24);
To keep it simple I just subtract 24 hours from the unix timestamp.
A modern oop-approach is using DateTime
$date = new DateTime();
$date->sub(new DateInterval('P1D'));
echo $date->format('F j, Y') . "\n";
Or in your case (more readable/obvious)
$date = new DateTime();
$date->add(DateInterval::createFromDateString('yesterday'));
echo $date->format('F j, Y') . "\n";
(Because DateInterval is negative here, we must add() it here)
See also: DateTime::sub() and DateInterval
shareimprove this answer
   
Thanks for the quick response. – Alex Jul 22 '11 at 23:00
2 
very good answer, there are many ways to do it but you have the simple one, short and easy to understand ( + the OOP one for those interested in using more RAM ;) – neofutur Apr 12 '12 at 8:04
2 
@neofutur Beside "we want to waste our RAM" the OOP-approach is useful, when you already have either aDateTime-, or a DateInterval-object (or both), which may be the case in an OOP-based application (e.g. $customer->expire->add(DateInterval::createFromDateString('+1 year')) // customer paid for one additional year) Also note, that DateInterval understands ISO8601 for time intervals, but date() doesn't. At the end of course: Select the one you better fit your needs :) – KingCrunch Apr 12 '12 at 8:19 
8 
This is NOT a correct answer!! Daylight savings makes 23 and 25 hour days. So this is NOT giving you a good answer 2 hours of the year!!! – patrick Apr 24 '13 at 15:25
3 
@patrick Interesting point :) Yes, you are right, but this is only true for the first solution, not the following utilizing DateInterval. – KingCrunch Apr 25 '13 at 14:26
strtotime(), as in date("F j, Y", strtotime("yesterday"));
shareimprove this answer
10 
+1 because this makes the intent a bit more obvious when scanning through the code. Self-documenting code and all that. – Justin ᚅᚔᚈᚄᚒᚔ Jul 22 '11 at 22:48

How easy :)
date("F j, Y", strtotime( '-1 days' ) );

lunes, 15 de febrero de 2016

PHP MySQL Date Range Search Filter

http://phppot.com/php/php-mysql-date-range-search-with-jquery-datepicker/
<?php
$conn = mysqli_connect("localhost", "root", "", "blog_samples");

$post_at = "";
$post_at_to_date = "";

$queryCondition = "";
if(!empty($_POST["search"]["post_at"])) {
$post_at = $_POST["search"]["post_at"];
list($fid,$fim,$fiy) = explode("-",$post_at);

$post_at_todate = date('Y-m-d');
if(!empty($_POST["search"]["post_at_to_date"])) {
$post_at_to_date = $_POST["search"]["post_at_to_date"];
list($tid,$tim,$tiy) = explode("-",$_POST["search"]["post_at_to_date"]);
$post_at_todate = "$tiy-$tim-$tid";
}

$queryCondition .= "WHERE post_at BETWEEN '$fiy-$fim-$fid' AND '" . $post_at_todate . "'";
}

$sql = "SELECT * from posts " . $queryCondition . " ORDER BY post_at desc";
$result = mysqli_query($conn,$sql);
?>

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