PROGRAMMING     CLIMBING

  minte9
reminder




Last update:   29-10-2021

Headers

A HTTP request/response is done in 3 steps: 1. The HTTP request is sent from a client 2. Server decodes the data and send it to PHP interpreter 3. Finally, Server send response data to the client

Redirects

You can use header() function to overwrite standard headers. This function must be called before any other output. The most common use of headers is to redirect the user to another page.
 
header("Location: http://new.com");

php.ini

You can set php.ini in order to be able to output data after you send body content.
 
# php.ini
output_buffering = 4096

Compresion

Compresion can make as much as 90% decrease in file size. But it uses more resources.
 
ob_start("ob_gzhandler"); // first line in php file
You can enable compresion on server side (can be easily turned on/off).
 
# php.ini
zlib.output_compression = on (off default)
zlib.output_compression_level = 9 (-1 default)

Cache

This will keep the page in browser's cache for 30 days
 
$date = gmdate("D, j M Y H:i:s", time() + 30*3600*24);
header("Expires: " . $data . " UTC");
header("Cache-Control: Public");
header("Pragma: Public");

Authentication

Authentication Required message to the client
 
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="Login"');
    header('HTTP/1.0 401 Unauthorized');
} else {
    echo $_SERVER['PHP_AUTH_USER'];
    echo $_SERVER['PHP_AUTH_PW'];
}

Export CSV

Save csv file from array.
 
declare(strict_types = 1);

class ExportCsv
{
    private $separator;
    private $exportFlag;
    private $filename;
    private $items;

    public function __construct(array $params)
    {
        $this->separator = @$params['separator'] ?: ",";
        $this->filename = $params['filename'];
        $this->exportFlag = $params['flag'];
        $this->items = $params['items'];
    }

    public function run() : void
    {
        if ($this->exportFlag == false) return; // no export
        if (!count($this->items)) return; // nothing to export

        $str = "";
        foreach($this->items as $k=>$v) { // rows
            $sep = "";
            foreach($v as $kk=>$vv) { // fields
                $str .= $sep . $vv;
                $sep = $this->separator;
            }
            $str .= "\r\n";
        }

        Header("Content-type: text"); // Look here
        Header(
            'Content-disposition: attachment; ' . 
            'filename="' . $this->filename . '"'
        );

        echo $str;
        die;
    }
}

$obj = new ExportCsv(array(
    'filename'    => "file.csv",
    'flag'        => true,
    'items'        => [
        ["name" => "John", "age" => 47],
        ["name" => "Mary", "age" => 35],
    ],
));

$obj->run();

Context

 
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
    'header'  => "Content-type: application/x-www-form-urlencodedrn",
    'method'  => 'POST',
    'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

Questions    
File Uploads
        A B C D E F
🔔
1/2