You Are Here Home > PHP

PHP

Store An External Web Page Into a Variable

You can use PHP’s file functions to do this:

$txt = file_get_contents('http://www.codingrecipes.com/');
Store An External Web Page Into a Variable
Comments (0)   Filed under: PHP   Posted by: Codehead

Get All The URLs In a Web Page

To do this, you need to use regular expressions:

$txt = file_get_contents('http://www.codingrecipes.com/');
preg_match_all('#<a\s+href=(?:"|\')(.+?)(?:"|\')#i', $txt, $matches);
foreach ($matches[1] as $url)
    echo "$url\n<br />";
Get All The URLs In a Web Page
Comments (0)   Filed under: PHP   Posted by: Codehead

Command Line Interface

Here it is in one line of PHP code:

<?php while (1) {  fputs(STDOUT, "\nPHP > "); eval(trim(fgets(STDIN))); } ?>

To run this, save it as php.php, and in command line run:

Widnows> php php.php
Linux> php.php

Command Line Interface
Comments (0)   Filed under: PHP   Posted by: Codehead

Get a Random Member of an Array

The best way to do this is using the array_rand function:

$days = array(1, 2, 3, 4, 5, 6, 7);
$random_day = $days[array_rand($days)];

The array_rand function returns a random key in a given array.

Get a Random Member of an Array
Comments (0)   Filed under: PHP   Posted by: Codehead

Codehead Search Engine API; how to write your own Search Application in ~150 lines of code

We launched Codehead Web Services a while ago and have introduced 2 APIs so far [A Website Thumnail API and a Search Engine API].

Our goal is to provide some cool functionality that is rather hard to implement through a set of simple web services/APIs so users can create and enhance their own applications.

Here is a full working search engine using the Codehead Search Engine API:

<?php
 
	error_reporting(E_ALL);
 
	/* Save sort order and match mode prefrences in a session variable */
	session_start();
 
	/* Set default values for sort, match_mode and search_section */
	if (!isset($_SESSION['sort']))
		$_SESSION['sort'] = 'relevance_date';
 
	if (!isset($_SESSION['match_mode']))
		$_SESSION['match_mode'] = 'any';
 
	if (!isset($_SESSION['search_section']))
		$_SESSION['search_section'] = 'articles';
 
	include 'libs/codehead_api/codehead_api.php';
	$ch = new Codehead_API();
 
	/* Assume there is a $_GET['q'], @ makes sure PHP won't trow E_NOTICE and if there is no $_GET['q'] then $q will be empty or '' */
	$q = trim(@$_GET['q']);
 
	/* $rpp is results per page */
	$rpp = 25;
 
	/* $start is where to start the next page; at what item number? 10, 20, 50? This usually comes from a pager */
	$start = intval(@$_GET['start']);
 
	/* Whether or not the user want to change sort, match_mode or search section */
	if (isset($_GET['sort']))
		$_SESSION['sort'] = $_GET['sort'];
 
	if (isset($_GET['match_mode']))
		$_SESSION['match_mode'] = $_GET['match_mode'];
 
	if (isset($_GET['search_section']))
		$_SESSION['search_section'] = $_GET['search_section'];
 
?><!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search - Powered By Codehead Search API</title>
 
	<style type="text/css">
		input { vertical-align: middle; }
	</style>
 
</head>
 
<body>
 
<!-- The search form -->
<form method="get" action="">
Search For: <input type="text" name="q" value="<?php echo __escape($q); ?>" /> <input type="submit" value="  GO  " />
</form>
Search in:
	<a href="?q=<?php echo __escape($q); ?>&search_section=articles">Articles</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=forums">Forums</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=jobs">Jobs</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=news">News</a> -
   <a href="?q=<?php echo __escape($q); ?>&search_section=blogs">Blogs</a> - It's '<?php echo $_SESSION['search_section']; ?>' right now
<br />
Sort by:
	<a href="?q=<?php echo __escape($q); ?>&sort=relevance_date">Relevance Date</a> -
   <a href="?q=<?php echo __escape($q); ?>&sort=relevance">Relevance Only</a> -
   <a href="?q=<?php echo __escape($q); ?>&sort=date">Date Only</a> - It's '<?php echo $_SESSION['sort']; ?>' right now
<br />
Matching Mode:
	<a href="?q=<?php echo __escape($q); ?>&match_mode=any">Any Keyword</a> -
   <a href="?q=<?php echo __escape($q); ?>&match_mode=all">All Keywords</a> -
   <a href="?q=<?php echo __escape($q); ?>&match_mode=exact">Exact Match</a> - It's '<?php echo $_SESSION['match_mode']; ?>' right now
<br /><br />
 
<?php
 
	/* If $q is not empty, do the search */
	if ($q != '') {
 
		/* Do the actual search, note how simple and painless it is! */
		$results = $ch->search->do_search($_SESSION['search_section'], $q, $start, $rpp, $_SESSION['sort'], $_SESSION['match_mode']);
 
		/* Error checking is always necessary! */
		if ($ch->has_error) {
 
			echo 'OOPS! there was a temporary error.';
			/* If you want to display the internal error, go ahead and do */
 
		} else {
 
			/* All good, loop through and display the results */
			// echo '<pre>'; print_r($results); exit;
 
			if ($results['total'] > 0) {
 
				echo "Results $start to " .($start + $rpp) ." out of around {$results['total']} total results; search took {$results['search_time']} second(s)!<br /><br />";
 
				/* Show the previous/next page link */
				if ($results['total'] > $rpp && ($start + $rpp) < $results['total'])
					echo "<a href=\"?q=$q&start=" .($start + $rpp) ."\">Next $rpp results</a>";
 
				if ($start >= $rpp)
					echo " - <a href=\"?q=$q&start=" .($start - $rpp) ."\">Previous $rpp results</a><br /><br />";
				else
					echo "<br /><br />";
 
				foreach ($results['results'] as $id => $result) {
					/* Very rarely, some items might be deleted from the index so this check is necessary */
					if (!isset($result['title']))
						continue;
				?>
					<a href="<?php echo __escape($result['url']); ?>"><?php echo __escape($result['title']); ?></a><br />
					<small>From: <?php echo __escape($result['source_title']); ?> <?php echo __escape($result['formated_date']); ?></small><br />
					<?php echo __escape($result['content']); ?><br />
				<?php
					/* Codehead Search groups results for news stories */
					if (isset($result['grouped'])) {
						foreach ($result['grouped'] as $gresult) {
						?>
							<small><a href="<?php echo __escape($gresult['url']); ?>"><?php echo __escape($gresult['title']); ?></a> - <span style="color: #999999;"><?php echo __escape($gresult['source_title']); ?></span></small><br />
                  <?php
						}
					}
					echo '<br />';
				}
 
				/* Show the previous/next page link */
				if ($results['total'] > $rpp && ($start + $rpp) < $results['total'])
					echo "<a href=\"?q=$q&start=" .($start + $rpp) ."\">Next $rpp results</a>";
 
				if ($start >= $rpp)
					echo " - <a href=\"?q=$q&start=" .($start - $rpp) ."\">Previous $rpp results</a>";
 
			} else {
				echo 'Your search didn\'t match any documents.';
			}
 
		}
 
	}
 
?>
 
</body>
</html>
 
<?php
 
	function __escape($str) {
		return htmlentities($str, ENT_QUOTES, 'utf-8');
	}
 
?>

It’s very easy to understand this code but I’ll try to explain a few things about how it works.

Here is the full documentation of our Search Engine API:

  • Simple Usage

    Here is the simplest way of searching our database for the term ‘PHP Sessions’:

    <?php
     
    /* Include and instantiate Codehead_API here, if you don't know how, read from the beginning of this documentation */
     
    $results = $ch->search->do_search('articles', 'PHP Sessions');
     
    if (!$ch->has_error) {
    	if ($results['total'] > 0) {
     
    		foreach ($results['results'] as $id => $result) {
    			/* Very rarely, some items might be deleted from the index so this check is necessary */
    			if (!isset($result['title']))
    				continue;
    		?>
    			<a href="<?php echo __escape($result['url']); ?>"><?php echo __escape($result['title']); ?></a><br />
    			<small>From: <?php echo __escape($result['source_title']); ?> <?php echo __escape($result['formated_date']); ?></small><br />
    			<?php echo __escape($result['content']); ?><br />
    		<?php
    			/* Codehead Search groups results for news stories */
    			if (isset($result['grouped'])) {
    				foreach ($result['grouped'] as $gresult) {
    				?>
    					<small><a href="<?php echo __escape($gresult['url']); ?>"><?php echo __escape($gresult['title']); ?></a> - <span style="color: #999999;"><?php echo __escape($gresult['source_title']); ?></span></small><br />
    				<?php
    				}
    			}
    			echo '<br />';
    		}
     
    	} else {
    		echo 'Your search didn\'t match any documents.';
    	}
    }
     
    ?>
    <br />
    			The function __escape() is not necessary but we like to escape EVERYTHING before printing, this function is very simple:<br /><br />
    <pre lang="php">
    <?php
     
    function __escape($str) {
    	return htmlentities($str, ENT_QUOTES, 'utf-8');
    }
     
    ?>

  • Methods

    There are 6 methods that you can use to retrieve search results:

    <?php
     
    array do_search(string $section, string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
     
    array articles(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array jobs(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array forums(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array blogs(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
    array news(string $search_term [, int $offset [, int $results_per_page [, string $sort_mode [, string $match_mode]]]] )
     
    ?>

    Below are detailed descriptions of these arguments and the reply from the server…

  • Search Sections

    Codehead Search Engine API has 5 sections for searching:

        1 – forums
        2 – articles
        3 – blogs
        4 – news
        5 – jobs

    You could search these sections directly like:

    <?php
     
    $results = $ch->search->jobs('Python');
     
    ?>

    Or use the generic do_search method like:

    <?php
     
    $results = $ch->search->do_search('jobs', 'Java');
     
    ?>

    !!!Please note that, the default section is ‘articles’ so if you misspell the section or pass in an empty section, the API will respond with all the articles matching your term!!!

  • Offset and Results Per Page (RPP)

    The offset is the start offset in which you want the results; if you have a pager and a user clicks on ‘Page 2′ then you must pass in the starting offset of the page 2. The default value for offset is 0 which means starting at the beginning of the result set.

    Results per page (RPP) is the number of search results that you want to show on each of your SERP pages. Valid values for RPP are 10 or 25 and the default value is 10.

    As an example, suppose you are showing 10 search results per page so the starting offset of your page 3 would be 3 * 10 or 30 so:

    <?php
     
    $results = $ch->search->do_search('news', 'bing', 30, 10);
     
    ?>

    Will display the page 3.

  • Sort Mode

    This is the mode in which the Search Engine API will sort your search results and valid values are:

    relevance_date

    Which will sort your results in order of relevance to the search term and also their freshness, this is our prefered mode.

    relevance – Also Default

    Will only sort your results based on their relevance to the search term. This is the default value for $sort_mode.

    date

    Will only sort your results based on date and it’s the least favorite sorting mode.

    Examples

    <?php
     
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'date');
     
    ?>

  • Match Mode

    There are 3 matching modes:

    any

    This will tell the Search Engine API to match any of the keywords in your search term, for example if your search term is ‘SEO Jobs’, the API will return all the documents with either ‘SEO’ or ‘Jobs’ in them and that will include documents with both keywords in them too.

    all – Also Default

    Will return all the documents with all the keywords in them. This is also the default $match_mode.

    exact

    This will match all the documents with all the keywords in them but this time it will make sure that the keywords will appear in the exact order in the search term. This is basically phrase match, for example if your search term is ‘PHP Jobs Las Vegas’, the Search Engine API will return all the documents with the exact phrase ‘PHP Jobs Las Vegas’ in them.

    Examples

    <?php
     
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date', 'any');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date', 'all');
    /* or */
    $results = $ch->search->do_search('news', 'bing', 30, 10, 'relevance_date', 'exact');
     
    ?>

  • All About The Results Array

    The return value of any of the above methods is an array that contains the search results along with some data about the search.

    The components of this array are:

        1 – results – Which is an array containing the search results.
        2 – total – Is the total number of matches, this is useful when building pagers.
        3 – encoding – Is the encoding of the items in the result.
        4 – time_now – The current time of the server in Unix timestamp.
        5 – start – The start offset of the results.
        6 – rpp – Results per page.
        7 – sort – Sort mode.
        8 – match_mode – Match mode.
        9 – search_time – The time it took to perform the search in milliseconds.

    !!!Please note that, each item in the ‘results’ might contain another sub-array named ‘grouped’ which are the results that were similar to that item; this will only be the case when you search the news!!!

    The best way to examine the ‘results’ array is to perform a search and then print_r() the results.

    <?php
     
    $results = $ch->search->do_search('blogs', 'snow leopard', 30, 10, 'relevance_date', 'any');
    print_r($results);
     
    ?>

Any problems with the API? You can create a support ticket in your Codehead Webservice Account under Account > Help.

Good Luck :)

P.S. Check out the original Codehead Search, our super-fast, custom search engine, where you can scour the best content on the net for computer and web related jobs, forum posts, articles and tutorials, blog posts and news stories all in one place.

Codehead Search Engine API; how to write your own Search Application in ~150 lines of code

How to write a permission system using bits and bitwise operations in PHP

I wrote this in PHP but you can use the same concept in other languages, I also assume an understanding of bits, bytes, binary to decimal conversion and vice-versa and bitwise operations on numbers like ‘or’, ‘and’ and ‘xor’ etc. if you have no idea, search and read about these first. You don’t have to be a guru but you should have an idea. Here are some pages to get you started:

http://en.wikipedia.org/wiki/Byte
http://en.wikipedia.org/wiki/Bitwise_operation
http://us.php.net/manual/en/language.operators.bitwise.php
Some binary to decimal calculators to make it easier

We will use simple numbers to represent different permissions and as you might know a number is a collection of bytes. For example: an integer is usually 4 bytes. Although you don’t have to worry about the size of a number in a high level language like PHP but a little understanding of representation of numbers will help you better understand this technique.

So let’s assume when I say:

<?php
 
	$user_perms = 7;
 
?>

Internally the variable $user_perms looks like this:

|0|0|0|0|0|0|0|0|0|0|0|0|0|1|1|1|

This is a 2 byte representation of number 7, although, it might not look like this internally – it looks similar. Just assume this for now.

Let’s say that your application supports 4 functions that a user can use:

1 – Post a blog post
2 – Comment on blog posts
3 – Edit posts
4 – Delete posts

Normally, you could have 4 fields in your database table (structure or whatever) for a user titled:

1 – can_post
2 – can_comment
3 – can_edit
4 – can_delete

This is not good, 4 additional fields for your user table and who knows, what if your application has 100 functions? Do you want to add 100 fields to your user table?

With bits, you can have only 1 column and track all the permissions.

1 – perms

To do this, we will have to assign numbers for each of the functions: (Tip: use one of the calculators in the above list ;) )

1 – Post a blog post |0|0|0|0|0|0|0|1| is 1 in decimal
2 – Comment on blog posts |0|0|0|0|0|0|1|0| is 2 in decimal
3 – Edit posts |0|0|0|0|0|1|0|0| is 4 in decimal
4 – Delete posts |0|0|0|0|1|0|0|0| is 8 in decimal

So you could have an array like this:

<?php
 
	$perms = array(
		'can_post' => 1,
		'can_comment' => 2,
		'can_edit' => 4,
		'can_delete' => 8
	);
 
?>

Almost there, let’s look at user’s perms field now.

I hope you know about bitwise ‘or’, when you ‘or’ 1 and 1 you get 1; 0 ‘or’ 1 is 1; 1 ‘or’ 0, is 1 and finally 0 ‘or’ 0 is 0, it’s just like the meaning of ‘or’ in the English language.

Similarly, bitwise ‘and’; when you ‘and’ 1 and 1 you get 1; 0 ‘and’ 1 is 0; 1 ‘and’ 0, is 0 and finally 0 ‘and’ 0 is 0, again it’s just like the meaning of ‘or’ in the English language.

Bitwise ‘xor’; when you ‘xor’ 1 and 1 you get 0; 0 ‘xor’ 1 is 1; 1 ‘xor’ 0, is 1 and finally 0 ‘xor’ 0 is 0.

So suppose you want to give a user permissions to post a blog post, post a comment and edit posts but not delete posts, you do it like this:

<?php
 
	$user_perms = $perms['can_post'] | $perms['can_comment'] | $perms['can_edit'];
 
?>

Note that, in PHP ‘|’ means ‘or’, so what just happened is something like this:

|0|0|0|0|0|0|0|1| ‘or’
|0|0|0|0|0|0|1|0| ‘or’
|0|0|0|0|0|1|0|0|
_______________________
|0|0|0|0|0|1|1|1|

Now $user_perms has the value 7 and |0|0|0|0|0|1|1|1| in it internally.

Suppose that this is on top of your post_blog.php or where ever you want to handle permissions for posting a blog, the only thing you need to do is:

<?php
 
	if ($user_perms & $perms['can_post']) {
		/* He/She has permissios to do this */
	} else {
		/* He/She doesn't */
	}
 
?>

In PHP ‘&’ is for bitwise ‘and’, please also note that ‘&&’ is logical ‘and’ and doesn’t operate on individual bits.

This is exactly what just happened:

|0|0|0|0|0|1|1|1| ‘and’
|0|0|0|0|0|0|0|1|
_______________________
|0|0|0|0|0|0|0|1|

So that’s ‘one’ not ’0′, which means ‘if’ passes and the user has permissions to do this. But when it comes to deleting posts:

<?php
 
	if ($user_perms & $perms['can_delete']) {
		/* He/She does permissios to do this */
	} else {
		/* He/She doesn't */
	}
 
?>

Thus:

|0|0|0|0|0|1|1|1| ‘and’
|0|0|0|0|1|0|0|0|
_______________________
|0|0|0|0|0|0|0|0|

It’s ‘zero’ so ‘if’ fails and you show an error message or whatever it is you do.

To add ‘delete’ permissions, you use ‘or’ again:

<?php
 
	$user_perms |= $perms['can_delete'];
 
?>

So this happens:

|0|0|0|0|0|1|1|1| ‘or’
|0|0|0|0|1|0|0|0|
_______________________
|0|0|0|0|1|1|1|1|

To take away permissions you use ‘xor’:

<?php
 
	$user_perms ^= $perms['can_delete'];
 
?>

And this will happen:

|0|0|0|0|1|1|1|1| ‘xor’
|0|0|0|0|1|0|0|0|
_______________________
|0|0|0|0|0|1|1|1|

And delete permissions are gone!

Now let’s take away post permissions:

<?php
 
	$user_perms ^= $perms['can_post'];
 
?>

Thus:

|0|0|0|0|0|1|1|1| ‘xor’
|0|0|0|0|0|0|0|1|
_______________________
|0|0|0|0|0|1|1|0|

So this was just the basics, you can build on this and do more once you understand.

I hope this post will help someone :)

How to write a permission system using bits and bitwise operations in PHP
Comments (10)   Filed under: PHP,Programming,Security,Web Development   Posted by: Codehead

The annoying PHP ZipArchive class

This is very rare, PHP’s extensions are usually well done but this one, ZipArchive class is very annoying and I’m very surprised that it’s now in PHP’s distribution…

In my case, it doesn’t create the archive for some reason and it doesn’t throw any errors either.

http://us3.php.net/manual/en/class.ziparchive.php

I’m very disappointed and I wasted a lot of time and yes, all the permissions etc. are right.

So ended up using system calls; first create a temporary folder and then something like:

system("zip -r THE_ZIP_FILE.zip PATH_TO_THE_TMP_FOLDER");

And now the problem with this is that it’s not portable, it’s fine in my case though.

The annoying PHP ZipArchive class
Comments (10)   Filed under: Annoying Stuff,PHP,Web Development   Posted by: Codehead

highlight_string/highlight_file doesn’t work

Make sure you wrap the PHP code with <?php and ?>

<?php
highlight_string('<?php
// ... Some code
?>');
?>
highlight_string/highlight_file doesn’t work
Comments (0)   Filed under: PHP   Posted by: Codehead

Send HTML emails

To send HTML emails, you will need special email headers:

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: FROM-EMAIL@YOURDOMAIN.COM\r\n"
                ."Reply-To: REPLY-EMAIL@YOURDOMAIN.COM";
mail('TO EMAIL ADDRESS', 'SUBJECT LINE', 'Message Body<br /><small>With some HTML</small>', $headers);
Send HTML emails
Comments (0)   Filed under: PHP   Posted by: Codehead

Generate a Unique String

$uid = uniqid();
Generate a Unique String
Comments (0)   Filed under: PHP   Posted by: Codehead
« Newer PostsOlder Posts »