Output Json Header In PHP

Overview of JSON

JSON http://www.json.org/ is an acronym for JavaScript Object Notation. It is essentially a way of exchanging data between a source and destination using name/value pairs. Commonly used when transferring data from a database to a website using AJAX.

Generating a JSON object using php is pretty simple (although it won’t be covered in this article), the lesser known technique is setting the correct content type so that the php page that generates the JSON object is seen as an actual JSON object rather than a plain text/html page.

Some libraries such as jQuery.getJSON() will detect wether or not the html page is a json response by looking at the Content-Type header of the response.

Json Content Type

What I mean by the correct content type is that each file type has its own content type which tell the browser how to handle the file. It informs the browser of what type of file follows so the browser is more aware on how to handle the content. For example a css file is seen as text/css where as an html or php file is seen as a text/html and likewise an image file can be image/jpg or image/png.

For a complete list of mime-types view this article or if you want to learn how to determine a mime-type of a file, click here.

Output Json Header In PHP

As for JSON, it can be output as a javascript file but also output as a json file. To overwrite the default content of text/html that the php file will most likely be, we will set the correct headers using php’s header(); function as you can see below.

Output as Json Using PHP Headers

<?php

// set our content-type to json
//  note that a Content-type of application/javascript works just as well as application/json
header('Content-Type: application/json');

// now output our json object
echo '{"name":"darian","lastname":"brown","age":87,"adress":{"21 somewhere street","my city","Australia"}}';

?>

Now when the page is loaded, the page will be seen as a JSON script rather than a simple html page.

Convert PHP Array to JSON Object

It is very easy to convert a php array into a json object using the php function json_encode(). It simply takes a multidimensional php array and converts it into a valid json string that will be read as a json object by javascript.

<?php

// create our php array - this will convert to a json object
$array = array(
	"name" => "darian",
	"lastname" => "brown",
	"age" => 87,
	"address" => array(
		"21 somewhere street",
		"my city",
		"Australia"
	)
);

// now we convert the above array to json string using json_encode()
$json = json_encode($array);

// set the json header content-type
header('Content-Type: application/json');
// and output the $json string so that it can be handled by our javascript code as a json object
echo $json;

// above echo will output our array as
//	'{"name":"darian","lastname":"brown","age":87,"adress":{"21 somewhere street","my city","Australia"}}'

?>

Calculate Time Difference Between Your End User And Your Server

Overview

People access your site aren’t always in the same timezone as you are. Now perhaps you would like to determine what the time is for a user in a different country. Then here is a quick way to find this information out.

This example is very useful if you simply want to fetch your clients date and time or timezone; but also good to see what time of day (their time, not server time) your clients access your website.

Now before I should you the code, remember that PHP is server side code – ie. it runs on the server and as such doesn’t have access to the clients local time. Javascript however is client side code which means it can see the localtime but the servers time. So a combination of the two will allow us to calculate the difference.

How To Calculate Time Difference

The way the below example works, is that the client will click a submit button which sets the value of the hidden input field to their local time stamp and that gets submitted to the same php script. Then the php script checks if that value has been sent and if it has, then it gets the local time (of the server) and takes the clients times and shows a message depicting the difference.

<!DOCTYPE html>
<html>
<head>
	<title>Server and Client Time Example</title>
</head>
<body>
	<script type='text/javascript'>

	// when we submit the form, get the client time and submit it as the
	// hidden field 'client_timestamp'
	function getClientTime() {
		var d = new Date();
		// we divide the timestamp by 1000 to go from milliseconds to seconds
		var timestamp = Math.floor(d.getTime()/1000);

		// put the timestamp into hidden field to be passed with the form
		document.getElementById('client_timestamp').value = timestamp;
	}

	</script>

	<form method='post' id='myform' onsubmit='getClientTime();'>
		<input type='hidden' id='client_timestamp' name='client_timestamp' />
		<button type='submit'>Send My Time</button>
	</form>

	<?php

	// if the form with the timestamp was submitted
	if( isset( $_REQUEST['client_timestamp'] ) ) {
		// the time posted to this script through the form
		$client_time = $_REQUEST['client_timestamp'];

		$server_time = time();

		$client_string = 'Your clients time is: ';
		$client_string .= date('d F Y h:i:s A',$client_time);

		$server_string = 'Your server time is: '
		$server_client .= date('d F Y h:i:s A',$server_time);

		// display the times
		echo $client_string.'<br />';
		echo $server_string.'<br />';
	}

	?>

</body>

</html>

Why Unix Timestamp Is Useful

Overview

Unix timestamps are a way or storing a specific date and time. Unix timestamps are a 10 digit number that represents the number of seconds that have passed since midnight Universal Co-ordinated Time (UTC) of January 1st, 1970.

What makes timestamps ever so useful is that since timestamps are recorded as of UTC (or GMT) a single time stamp can be used to to represent all time zones. Once you have your timezone, the unixtime stamp will adjust the time accordingly. For example, a unix timestamp of 1304951846 can represent 10th May at 0:37 AM in London, but it would also represent 10th May at 10:37 AM in Brisbane/Australia.

Why Unix Timestamp Is Useful

How can this be useful? Just think, users can see dates and times on your website relative to their own timezone, rather than the timezone of the server the website is being run on. Being a web developer, I am forever finding myself storing dates do be displayed back to the user. It may be a date a user last logged into a program or website, perhaps even a date that a user posted an article such as this to a website. Once we store a specific timestamp, we can display the time and date to users but also in the correct relevant timezone. On top of which, timestamps are easy to use, and many languages support them.

Online Timestamp Converter

There are online unix timestamp generators to convert to and from unix time stamps such as the one found here.

PHP can convert a timestamp to a human readable format by using the date() function and likewise using strtotime() and mktime() to convert a natural language string to a unixtime stamp to be stored. Short examples are:

PHP Timestamp Example

<?php

/* convert unixtime stamp to human time */
echo date('d F Y H:i:a',1304951846); // would display a time of 10 May 2011 00:37:am

/* convert to a unix time stamp */
echo strtotime('+1 Year'); // would return the timestamp of 1 year from today
echo mktime($hour,$min,$sec,$month,$day,$year); // echo a timestamp specified by the variables

?>

MySQL Timestamp Example

MySQL uses the syntax FROM_UNIXTIME to convert data to a human readable format for example:

SELECT *,FROM_UNIXTIME(timestamp_column) FROM `table`;

Free Online Unix Timestamp Converter

Generate your own unix timestamp online using an Online Unix Timestamp generator.

Convert Unix Time Online Website

Overview

After discovering exactly what timestamps are and how much they can simplify storing dates and time, I often find myself using unix timestamps more often than not. If you are new to timestamps, take a look at this article and see what timestamps are and how they are useful.

Unix timestamps are great and I can’t live without them but the issue with timestamps is that a timestamp is simply a 10 digit number (e.g. 1307405403).

Convert Unix Time Online Website

I have created a website that allows me to quickly convert a unix timestamp online to an easy to read date as well as being able to convert a date back into a timestamp. It has helped me out tremendously which is why I have now made this tool available for the rest of the community to use and I hope that it makes other people’s life easier.

There are also article on that site which demonstrate Real World Usage Examples Of Timestamps as well as giving you Interesting Facts About Timestamps.

Read more…

Online DNS Dig Website

Overview

For those people who find themselves playing around with DNS setups from time to time will more than likely have worked with or at least heard of the command `DIG`.

For those who don’t know what DIG is or think of it as the necessary first step to hiding a body, will be surprised to know that DIG is a command that is used to directly query a DNS server to find out specific information about a domain name or sub domain. This can be useful when you are trying to setup a new domain or simply check if a sub domain already exists.

Further to this, you can check many types of records such as mail records (used in creating a mail server), CNAME records (points one domain name to another domain name) and most commonly is the A record (domain pointing to an IP address).

Online DNS Dig Website

Now there are times when I don’t have access to my Linux machine or perhaps to lazy to try remember 32 bit password i seem to create and couldn’t get access to the normal dig tool. Here is what lead me to create a website that would allow me to easily perform a dns look up and execute a web based dns dig online. Simply enter the domain you are querying, the type of record to look up and optionally enter the dns server and off you go.

Check Out The Online DNS Dig Tool

You too can use the site by clicking here. Please note that this site is still in it’s early release and may perform unexpectedly.

Read more…

Export Proxy Username Password Linux

Overview

If you are wondering how you can tell your Linux applications to use a proxy, then you have found the right article and as you will find out, it is surprisingly simple. After you set the proxy in linux, any applications such as wget, lynx and ftp will then be redirected through the proxy.
There are two settings you can make. One for web (http_proxy) and the other is ftp (ftp_proxy). We do this by setting each one in our environment variable.

Setup Proxy in Linux

Here is the most basic setup below. It will set an environment variable called http_proxy and ftp_proxy which will cause all your applications to redirect through the proxy.

export http_proxy='http://example.com'
export ftp_proxy='ftp://example.com'

Export Proxy Username Password Linux

Here we set the username and password for the proxy by passing it before the domain and seperate with a colon.

export http_proxy='http://user:password@example.com'
export ftp_proxy='ftp://user:password@example.com'

Set A Different Port To Use With The Proxy

Your proxy can run on a different port, so to select an alternate port, you can pass the port number after the domain. Below we select 8080 as the port for our proxy.

export http_proxy='http://example.com:8080'
export ftp_proxy='http://example.com:8080'

Make Linux Proxy Export Permanent

Something to be aware of, is that setting this environment variable only makes it available until you log out. If you would like to make the export proxy setting permanent, then add any of the lines above to your .bashrc or .profile file in your home directory which is found under /home/yourfolder/.bashrc or /home/yourfolder/.profile or ~/.bashrc or ~/.profile (since ~ is just a shortcut to your home directory). Now each time you login, it will automatically set this environment variable for you but not any other users.

Set Proxy For All Users

In order to setup the proxy for all users on your Linux system, you need to set the export the environment variable to all users when they login by adding a line to /etc/bash.bashrc. Now when any user logs in, they will automatically have the proxy setup for them.

MySQL REPLACE – Replacing text in MySQL

Overview

MySQL replace is a mysql function that allows you to replace one bit of text with another piece of text during a MySQL query. The best way to understand how a MySQL REPLACE Function works is to see it work as you will see in the example below.
The MySQL replace function works similar to how the PHP str_replace function works however the parameters are in different order.

MySQL REPLACE

The function is called mysql replace() and takes 3 parameters which are:

  1. the mysql column name
  2. the text you want to be replace
  3. the text to replace the existing text with

For example, the function will look something like this:

REPLACE(column_name, 'replace_this', 'with_this_text');

Example 1 – SELECT and REPLACE

In this example, any results that have John as their first name will instead return Johnathon as their firstname and any other names will not be affected.

SELECT REPLACE(`firstname`,'John','Johnathon') AS firstname,lastname,age FROM userdatabase;

 

Example 2 – UPDATE and REPLACE

This will update and switch one bit of text with another. Here we are going to change some html and replace <i> tags with <em> tags.

UPDATE wp_posts SET post_content = REPLACE(post_content,'<i','<em');

Now all the italic html tags would have ben replace with emphasized tags.

Running PHP Script From Cron

Overview

Cron is a job based scheduler used in Unix type operating systems. It allows you to schedule tasks to be run at given intervals such as scheduling a php script to run weekly, every second day, hourly, every 3 minutes, weekends, and so on.
In this article, I will show how easy it is in running a php script from cron. It will also show you how you can run a php script via a url using wget.

Running PHP Script From Cron

I will show you 3 ways on running php script from cron which are:

  1. Running php script via command line in cron
  2. Running php script via php5-cgi in cron
  3. Running php script remotely via website url in cron

Running a php script usually requires the php script to have execute permissions set. Running PHP script from cron is usually a matter of specifying the file and as long as the script has execute permissions (i.e. chmod +x) then all should be good. Here are three ways I have found work very well.

Running PHP Script On Command Line From Cron

In order for the script to run correctly you should add the following to your php script. The informs the bash what type of script it is executing.

#!/usr/bin/php
<?php

//.. your code follows here

Next we add the entry into cron.

# you could output to a file by changing /dev/null to /some/file/to/save.log
*/10 * * * * /home/user/scripts/updatesomething.php > /dev/null

Note we didn’t need to execute the script via php (/usr/bin/php /path/to/script.php) since we added the header to the php script.

Running php script via php5-cgi in cron

To execute the php script from cron you need to have php5-cgi installed. You can install this on Debian use “apt-get install php5-cgi” or the equivalent command for your distribution of linux.

# PHP5-CGI: execute the php script every 2 minutes and send output to /dev/null
# you could output to a file by changing /dev/null to /some/file/to/save.txt
*/2* * * * php5-cgi -q /home/user/scripts/parselogs.php > /dev/null

Running php script via website url in cron

And lastly we will use wget from the cron to download a website url and thus, execute the url that the php script is located at. We do this in the cron to allow the url to be downloaded at set times. To use wget, you need to install it wget using “apt-get install wget” or the equivalent command for your distribution of linux.

# wget will visit the url but won't save the file. send it to /dev/null
# this executes the script as if you visited the link in your browser
0 * * * * wget -O /dev/null http://www.example.com/some_cron_script.php

And there we go, your php script will be run by cron on time, every time.

CSS Two Column Fluid Layout – Static Column With Elastic Fluid Column

Overview

Are you looking for some quick HTML code to create a two column table with one static column and an elastic or fluid column. It took me a while to figure out, especially since css is one of my weaker languages. So I have placed the code here to hopefully make your life easier when it comes to creating html static and liquid column layouts.
There are only two parts to achieve this and what you will find after creating the two pages (one css file and an html file) is a page that has a static right hand column of around 400px and a elastic, liquid left column (or main content) that will stretch to fit the width of the browser as you resize the window.

CSS Two Column Fluid Layout

Below is the css code used to create the two column table. The important part to take notice of is wrapper which has a 400px padding to the right. This allows the fixed right hand column to fit snug next to it without the right column dropping down below. Copy the below code into a separate css file (I named mine elastic.css) or include it inline in the same HTML file where the html code will be.
As you will see, there are some optional css parameters which are commented out. They are more for styling rather than being required. Use as needed.

body {
	font: 16px/24px Georgia, serif;
	color: #5A5A5A;
	margin: 0;
}

header, footer {
	/*padding: 30px 20px;  optional: add some padding to match the margins of the #wrapper */
	background: #555; /* optional: background colour */
	color: #F0F0F0;
}

#wrapper {
	padding-right: 400px; /* padding should be the total width of all right columns */
	/*margin: 0 20px;  optional: spacing to the side of our content and sidebars */
}

#content {
	float: left;
	width: 100%;
	min-width: 600px;
}

.sidebar {
	float: left;
	width: 400px;
	margin-right: -1000px;
	/*background: #DDD;  optional: background colour */
}

.sidebar .side-column {
	width: 200px;
	float: left;
	/*margin-left: 20px;  optional: add a small space between the columns as well as the content to the left */
}

footer {
	clear: left; /* clear the floating from the above content and sidebar this makes the footer appear correctly below */
}

HTML Code

Create your html file and paste the following code into it. If you decided to include the above css inline, then you will need to paste the above css code into the html code below.

<!DOCTYPE html>
<html>
<head>

<title></title>
<link rel="stylesheet" type="text/css" media="all" href="elastic.css" />

</head>
<body>
	<header><strong>Our Header</strong></header>
	<div id="wrapper">
		<div id="content">
			Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam auctor interdum elit faucibus scelerisque. Curabitur eleifend orci eget ante egestas viverra ac nec mauris. Pellentesque et dolor et leo pellentesque sollicitudin vitae vel velit. Phasellus ullamcorper sapien in sapien fermentum viverra. Sed dignissim est nec risus elementum molestie. Morbi placerat pharetra massa vitae aliquam. Curabitur laoreet suscipit leo, sit amet lacinia dolor placerat eu. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla facilisi. Nulla lacinia, ante ac dignissim viverra, nunc orci rhoncus odio, porttitor aliquet orci massa non lorem.
		</div>
		<div class="sidebar">
			<div class="side-column">
				Sed ornare sollicitudin nisi, in tristique quam laoreet non. Nullam semper, turpis et pulvinar pulvinar, quam dui consequat mi, ac aliquet arcu nunc id massa. Vivamus aliquam odio eget neque auctor tempus sollicitudin dui malesuada. Suspendisse tincidunt eleifend pellentesque.
			</div>
			<div class="side-column">
				Phasellus arcu nulla, pulvinar et cursus id, venenatis ac augue. Sed vitae erat in ante tristique cursus at non ante. Ut auctor metus at neque consectetur pulvinar. Morbi et mauris odio. Nulla viverra vulputate leo, vitae porta augue aliquam ac. Praesent ut nisi velit.
			</div>
		</div>
	</div>
	<footer><strong>Our footer</strong></footer>

</body>
</html>

After having created your two files, a css file and a html file, you can open the html file to see the result. You will notice that there are two columns in the html file, one being static/fixed width to the right hand side and the main content being liquid and is resized as the browser window is resized. It is a great way to make the most of your web browser window space and is rather simple to use (once you have the code samples).

PHP Get Mime Type From File Extension

Overview

Whenever you need to determine the mime-type of a file you should generally look at the file extension. Below is a pretty straight forward function that will return the mime-type of a file based on its file extension. Note! this is not a guaranteed way to determine the mime-type since people can easily fake the file extension. All you need to do is pass the php mime function a filename and it will try figure out what the mime type is.

PHP Get Mime Type From File Extension

Here is the function to retrieve the mime type of a file based on the file extension. See here for a list of mime-types.

<?php

function get_mime_type($file)
{

	// our list of mime types
	$mime_types = array(
		"pdf"=>"application/pdf"
		,"exe"=>"application/octet-stream"
		,"zip"=>"application/zip"
		,"docx"=>"application/msword"
		,"doc"=>"application/msword"
		,"xls"=>"application/vnd.ms-excel"
		,"ppt"=>"application/vnd.ms-powerpoint"
		,"gif"=>"image/gif"
		,"png"=>"image/png"
		,"jpeg"=>"image/jpg"
		,"jpg"=>"image/jpg"
		,"mp3"=>"audio/mpeg"
		,"wav"=>"audio/x-wav"
		,"mpeg"=>"video/mpeg"
		,"mpg"=>"video/mpeg"
		,"mpe"=>"video/mpeg"
		,"mov"=>"video/quicktime"
		,"avi"=>"video/x-msvideo"
		,"3gp"=>"video/3gpp"
		,"css"=>"text/css"
		,"jsc"=>"application/javascript"
		,"js"=>"application/javascript"
		,"php"=>"text/html"
		,"htm"=>"text/html"
		,"html"=>"text/html"
	);

	$extension = strtolower(end(explode('.',$file)));

	return $mime_types[$extension];
}

?>

Usage Examples

Here are a couple of ways to use this function.

<?php

// simple calls to fetch mime type of a file
echo get_mime_type('example.doc'); // displays "application/msword"
echo get_mime_type('picture.jpg'); // displays "image/jpg"
echo get_mime_type('Harry_Potter.mov'); // displays "video/quicktime"

Advanced Example

This example is slightly more advanced. It will set the http header image/jpg and output the image file. It can be used for displaying an image from a php script.

<?php

$mime = get_mime_type('my_image_file.jpg'); // this will return "image/jpg"
header("Content-Type: $mime");
echo file_get_contents('my_image_file.jpg'); // output the image to the browser
?>