jueves, 31 de enero de 2019

PHP: How to send HTTP response code?

378
I just found this question and thought it needs a more comprehensive answer:
As of PHP 5.4 there are three methods to accomplish this:

Assembling the response code on your own (PHP >= 4.0)

The header() function has a special use-case that detects a HTTP response line and lets you replace that with a custom one
header("HTTP/1.1 200 OK");
However, this requires special treatment for (Fast)CGI PHP:
$sapi_type = php_sapi_name();
if (substr($sapi_type, 0, 3) == 'cgi')
    header("Status: 404 Not Found");
else
    header("HTTP/1.1 404 Not Found");
Note: According to the HTTP RFC, the reason phrase can be any custom string (that conforms to the standard), but for the sake of client compatibility I do not recommend putting a random string there.
Note: php_sapi_name() requires PHP 4.0.1

3rd argument to header function (PHP >= 4.3)

There are obviously a few problems when using that first variant. The biggest of which I think is that it is partly parsed by PHP or the web server and poorly documented.
Since 4.3, the header function has a 3rd argument that lets you set the response code somewhat comfortably, but using it requires the first argument to be a non-empty string. Here are two options:
header(':', true, 404);
header('X-PHP-Response-Code: 404', true, 404);
I recommend the 2nd one. The first does work on all browsers I have tested, but some minor browsers or web crawlers may have a problem with a header line that only contains a colon. The header field name in the 2nd. variant is of course not standardized in any way and could be modified, I just chose a hopefully descriptive name.

http_response_code function (PHP >= 5.4)

The http_response_code() function was introduced in PHP 5.4, and it made things a lot easier.
http_response_code(404);
That's all.

martes, 20 de noviembre de 2018

reading a file and printing specific values

<?php
// get contents of a file into a string
$filename = "./areacode.log";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

$elements=explode("\n",$contents) ;
foreach($elements as $value) {
if (stristr($value,"California ")) {   // If  Number  California  is found it will print all values co Calif
echo "$value<br>";
}

}
?>

Original File
Alabama(1)251

Alabama(1)256

Alabama(1)334

Alaska(1)907

Arizona (1)480

Arizona (1)520

Arizona (1)602

Arizona (1)623

Arizona (1)928

Arkansas(1)479

Arkansas(1)501

Arkansas(1)870

California (1)209

California (1)213

California (1)310

California (1)323

California (1)408

California (1)415


domingo, 11 de noviembre de 2018

json encode y decode

<?php

$arr=array(6000=>"blocked",6001=>40000);

$info=json_encode($arr);

print_r($info);

$info=json_decode($info,true);

foreach($info as $key=>$value){
echo "<br><br>";
echo  "$key=>$value <br>";
}


?>

domingo, 23 de septiembre de 2018

asterisk xml loteria

#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
set_time_limit(30);
require_once('/var/lib/asterisk/agi-bin/phpagi/phpagi.php');
$agi = new AGI();
$agi->answer();

$xmlstring=file_get_contents("http://190.131.219.198:5020/ApiRestBolivarApp/webresources/web.service.Bnet.bnetServiceSources/resultadosAyer");
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
//print_r($array);
//print_r($array[resultadoLoteria][0]);

foreach($array[resultadoLoteria] as $key=>$value) {

foreach($array[resultadoLoteria][$key] as $key=>$value) {
if(!is_array($value)) {

$value=str_replace (" " ,"_",$value );

if($key=='nombreLoteria'){

 
$agi->stream_file($value);
}
sleep(1);
if($key=='resultado'){
$agi->say_digits($value);
}

sleep(1);

if($key=='serie'){
$agi->stream_file("serie");

$agi->exec("sayalpha","$value");


}
}
}
}
?>

<resultadoLoterias>
<resultadoLoteria>
<nombreLoteria>DORADO MANANA</nombreLoteria>
<resultado>8276</resultado>
<serie> </serie>
</resultadoLoteria>
<resultadoLoteria>
<nombreLoteria>CHONTICO</nombreLoteria>
<resultado>9304</resultado>
<serie> </serie>
</resultadoLoteria>
<resultadoLoteria>
<nombreLoteria>LA FANTASTICA DIA</nombreLoteria>
<resultado>1238</resultado>
<serie> </serie>
</resultadoLoteria>
<resultadoLoteria>
<nombreLoteria>PAISITA DIA</nombreLoteria>
<resultado>4608</resultado>
<serie> </serie>
</resultadoLoteria>
<resultadoLoteria>
<nombreLoteria>PIJAO</nombreLoteria>
<resultado>9477</resultado>
<serie>HAZ22 </serie>

lunes, 17 de septiembre de 2018

Ajax Pagination with Search and Filter in PHP

https://www.codexworld.com/ajax-pagination-with-search-filter-php/

get long and lat from place

http://www.mapcoordinates.net/en

HTML5 Geo Location

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
    "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>