Skip to content

Instantly share code, notes, and snippets.

@apphp-snippets
apphp-snippets / gist:4013482
Created November 4, 2012 20:15
This code allows to remove all duplicate elements from an array using PHP array_unique() function.
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-remove-duplicates-in-array */
$input = array("a"=>"apple", "pear", "b"=>"apple", "orange", "avocado", "banana");
print_r($input);
$result = array_unique($input);
print_r($result);
?>
@apphp-snippets
apphp-snippets / gist:2759490
Created May 20, 2012 20:52
Send Mail using mail function in PHP
<?php
/* Source: http://www.apphp.com/index.php?snippet=php-get-remote-ip-address */
$to = 'myfriend@gmail.com';
$subject = 'Test Email';
$body = 'Body of your message here. You can use HTML tags also, e.g. <br><b>Bold</b>';
$headers = 'From: John Smith'."\r\n";
$headers .= 'Reply-To: from@email.me'."\r\n";
$headers .= 'Return-Path: from@email.me'."\r\n";
$headers .= 'X-Mailer: PHP5'."\n";
$headers .= 'MIME-Version: 1.0'."\n";
@apphp-snippets
apphp-snippets / Custom Error Pages in .htaccess
Created August 18, 2013 09:27
This example shows you how to define custom error pages in .htaccess file and also how to display the error page on your site.
# Sample 1: redirect errors to html files
ErrorDocument 400 /400.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 405 /405.html
ErrorDocument 408 /408.html
ErrorDocument 414 /414.html
ErrorDocument 500 /500.html
ErrorDocument 502 /502.html
@apphp-snippets
apphp-snippets / Search in Tags Set in MySQL
Created August 14, 2013 09:33
Lets say you have a table with field called "tags" that consists from tags separated by commas and you want to check whether it includes a required tag. Here the simplest ways of doing that.
-- source: http://www.apphp.com/index.php?snippet=mysql-search-in-tags-set#null
-- 1st way
SELECT *
FROM TABLE
WHERE FIND_IN_SET(" '.$tag.'", CONCAT(" ", TABLE.tags))
LIMIT 1;
-- 2st way
SELECT *
FROM TABLE
@apphp-snippets
apphp-snippets / Center Website Content with CSS
Created August 12, 2013 08:31
Today in the days of high-resolution widescreen displays, it's annoying to visit websites that are formatted like it's 1990's. aligned all the way to the left. Make sure your website doesn't suffer the same fate by forcing it;s content to center horizontally and vertically.
<style type="text/css">
/* source: http://www.apphp.com/index.php?snippet=css-center-site-content */
/* Center your website horizontally */
.wrapper{
width:960px;
display:table;
margin:auto;
}
/* Center certain content vertically */
@apphp-snippets
apphp-snippets / jQuery Loading Fallback
Created August 12, 2013 08:33
Nowadays majority of sites uses the jQuery JavaScript library. A lot of them also make use of Google's hosted version of the library from some reasons: faster loading, better cross site caching etc. However, what if there is ever a problem and jQuery is not loaded from Google? For example when you work on localhost and there is no Internet conne…
<!-- http://www.apphp.com/index.php?page=html_snippets -->
<!-- Grab Google CDN's jQuery and load local version if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">!window.jQuery && document.write('<script src="js/jquery-1.4.2.min.js"><\/script>')</script>
// source: http://www.apphp.com/index.php?snippet=php-get-country-by-ip
function getLocationInfoByIp(){
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = @$_SERVER['REMOTE_ADDR'];
$result = array('country'=>'', 'city'=>'');
if(filter_var($client, FILTER_VALIDATE_IP)){
$ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)){
$ip = $forward;
<!-- source: http://www.apphp.com/index.php?snippet=javascript-focus-on-load -->
<!-- This part goes into your body tag -->
<body OnLoad="document.myform.user.focus();">
<!-- You you just use the name of the form and field name within your form -->
<form name="myform">
Name: <input type="text" name="user" size="10">
Email: <input type="text" name="email" size="10">
</form>
@apphp-snippets
apphp-snippets / Multiple File Input in HTML
Created February 11, 2013 08:43
File inputs can have an attribute of "multiple" which then allows multiple files to be selected in the file section dialog box. Currently only Firefox 3.6+ and WebKit browsers are supporting it.
<!-- source: http://www.apphp.com/index.php?snippet=html-multiple-file-input -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input name="upload_files[]" type="file" multiple>
<input type="submit" value="Upload">
</form>
<?php
// PHP code
that shows how to loop through the data as an array
foreach($_FILES["upload_files"]["name"] as $file){
@apphp-snippets
apphp-snippets / Media Code in HTML5
Created February 11, 2013 08:42
This example is a basic design structure for loading multiple video and audio formats for universal work of media content on your page.
<!-- source: http://www.apphp.com/index.php?snippet=html-5-media-code -->
<video poster="images/preview.png" width="800" height="600" controls="controls" preload="none">
<source src="media/my_video.mp4" type="video/mp4"></source>
<source src="media/my_video.webm" type="video/webm"></source>
<source src="media/my_video.ogg" type="video/ogg"></source>
</video>
<audio controls="controls" preload="none">
<source src="audio/my_music.ogg" type="audio/ogg">
<source src="audio/my_music.mp3" type="audio/mpeg">
</audio>