Tag: php

PHP

How to force page not to be cached in PHP

header function header(’Expires: Fri, 01 Jan 1990 00:00:00 GMT’); header(’Cache-Control: no-store, no-cache, must-revalidate, max-age=0, pre-check=0, post-check=0’); header(’Pragma: no-cache’); //…header(‘Expires: Fri, 01 Jan 1990 00:00:00 GMT’); header(‘Cache-Control: no-store, no-cache, must-revalidate, max-age=0, pre-check=0, post-check=0’); header(‘Pragma: no-cache’); //…

PHP

Parse JSON data in PHP

json_decode $data='{"id":1,"firstname":"george","lastname":"karpouzas","emails":["abcd@gmail.com","abcd@yahoo.com"]}’; $obj=json_decode($data); echo ‘ID: <b>’.$obj->id.'</b><br>’; echo ‘First name: <b>’.$obj->firstname.'</b><br>’; echo ‘Last name: <b>’.$obj->lastname.'</b><br>’; echo ‘Email 1: <b>’.$obj->emails[0].'</b><br>’; echo ‘Email 2: <b>’.$obj->emails[1].'</b><br>’;$data='{"id":1,"firstname":"george","lastname":"karpouzas","emails":["abcd@gmail.com","abcd@yahoo.com"]}’; $obj=json_decode($data); echo ‘ID: <b>’.$obj->id.'</b><br>’; echo ‘First name: <b>’.$obj->firstname.'</b><br>’; echo ‘Last name: <b>’.$obj->lastname.'</b><br>’; echo ‘Email 1: <b>’.$obj->emails[0].'</b><br>’; echo ‘Email 2: <b>’.$obj->emails[1].'</b><br>’; Output: ID: 1 First name: george Last name: karpouzas Email 1: abcd@gmail.com Email …

PHP

Create JSON data in PHP

json_encode Code: $data = array (’id’=>1, ‘firstname’=>’george’, ‘lastname’=>’karpouzas’, ’emails’=>array(’abcd@gmail.com’,’abcd@yahoo.com’));   echo json_encode($data);$data = array (‘id’=>1, ‘firstname’=>’george’, ‘lastname’=>’karpouzas’, ’emails’=>array(‘abcd@gmail.com’,’abcd@yahoo.com’)); echo json_encode($data); Output: {“id”:1,”firstname”:”george”,”lastname”:”karpouzas”,”emails”:[“abcd@gmail.com”,”abcd@yahoo.com”]}

PHP

List files in directory – PHP

$dir = ‘/path/to/dir/’; if(is_dir($dir)) { if($handle = opendir($dir)) { while(($file = readdir($handle)) !== false) { if($file != "." && $file != ".." && !is_dir($file)) { echo $file.'<br>’; } } closedir($handle); } }$dir = ‘/path/to/dir/’; if(is_dir($dir)) { if($handle = opendir($dir)) { while(($file = readdir($handle)) !== false) { if($file != "." && $file != ".." && !is_dir($file)) …

PHP

Send email with php

Sending email with PHP and build-in function mail. $to = "example@gmail.com"; $subject = "Test"; $body = "<b>Body</b> <i>of your message</i><br>"; $headers = "From: Georgern"; $headers .= "Reply-To: info@myserver.comrn"; $headers .= "Return-Path: info@myserver.comrn"; $headers .= "X-Mailer: PHP5n"; $headers .= ‘MIME-Version: 1.0’ . "n"; $headers .= ‘Content-type: text/html; charset=iso-8859-1’ . "rn"; mail($to,$subject,$body,$headers);$to = "example@gmail.com"; $subject = "Test"; …

Apache, PHP, Programming

Set PHP configuration options in Apache using directives

Apache directives to change the PHP configuration: php_value name value Sets the value of the specified directive. php_flag name on|off Sets a boolean configuration directive. php_admin_value name value Sets the value of the specified directive. This can not be used in .htaccess files. php_admin_flag name on|off Used to set a boolean configuration directive. This can …

PHP

require VS include & require_once VS include_once in PHP

include() The include() function includes and evaluates the file during the execution of the script. Files are included based on the file path given or the include_path. If the file isn’t found include() will check in the calling script’s own directory and the current working directory before failing. include() will emit a warning (E_WARNING) if …

PHP

How To Write A Simple PHP Web Service

Web Services are Everywhere! Let’s see how easy it is to create one using PHP, MySQL and XML! First of all create a mysql database named notes. Add a table named notes too: CREATE TABLE IF NOT EXISTS `notes` ( `ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `Title` VARCHAR(50) COLLATE utf8_unicode_ci NOT NULL, `Note` VARCHAR(500) COLLATE …