Building a URL Shortener from Scratch Using CodeIgniter

Overview

Todays post is a little longer and a lot more in-depth. Today we will look at setting up a URL shortening service with CodeIgniter similar to bit.ly etc. Yes there is 100s already out there, this is just to see how easy it is in CodeIgniter. You can create this service in minutes without breaking a sweat.

Continue reading “Building a URL Shortener from Scratch Using CodeIgniter”

Advertisements

Get the HTML of any page with PHP

Quick tip: if you want to get the HTML for any web page you can use the PHP function file_get_contents(). To see the HTML in your web browser wrap the htmlentities  function around the file_get_contents() function.

<?php

echo htmlentities(file_get_contents('http://example.com'));

Which returns:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>IANA &mdash; Example domains</title>
	<!-- start common-head -->
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="/_css/2008.1/reset-fonts-grids.css" />
	<link rel="stylesheet" type="text/css" media="screen" href="/_css/2008.1/screen.css" />
	<link rel="stylesheet" type="text/css" media="print" href="/_css/2008.1/print.css" />
	<link rel="shortcut icon" type="image/ico" href="/favicon.ico" />
	<script type="text/javascript" src="/_js/prototype.js"></script>
	<script type="text/javascript" src="/_js/corners.js"></script>
	<script type="text/javascript" src="/_js/common.js"></script>
	<!-- end common-head -->

</head>
<body>
	<!-- start common-bodyhead -->
....
	<!-- end common-bodytail -->

</body>
</html>

 

Increasing the PHP Memory Limit

Today I needed to run a some code that did a lot of heavy lifting and kept having the error below:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 71 bytes) in /Applications/MAMP/htdocs/test_memory.php on line 46

The solution is to override the memory limit set by PHP.ini:

<?php
ini_set('memory_limit','64M');

Check out PHP.Net’s ini_set documentation for more details.

Notes:

  • This has be done as one of the first commands in your PHP file
  • You may need to have to increase the limit to more if the problem is still occurring
  • If this is unexpected there may be some really inefficient code in your code base