Skip to content

Instantly share code, notes, and snippets.

@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 / 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>
@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 */
<!-- 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>
// 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;
@apphp-snippets
apphp-snippets / Selecting a Random Row from Table in MySQL
Last active December 12, 2015 09:39
Here the simplest way of selecting random rows from the MySQL database with using "ORDER BY RAND()" clause in the query.
-- source: http://www.apphp.com/index.php?snippet=mysql-selecting-random-row
SELECT * FROM TABLE
ORDER BY RAND()
LIMIT 1
@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>
@apphp-snippets
apphp-snippets / Resetting the Browser Default Styles in CSS
Created February 11, 2013 08:41
Using the same code in different browsers can be displayed in different ways. Resetting styles will help you to avoid such problems.
<!-- source: http://www.apphp.com/index.php?snippet=css-reset-browser-default-styles -->
<style type="text/css">
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code
, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video
{ margin:0; padding:0; border:0; font-size:100%; font:inherit; vertical-align:baseline; outline:none; }
html
{ height:101%; } /* always show scrollbars */
body
{ font-size:62.5%; line-height:1; font-family:Arial, Tahoma, Verdana, sans-serif; }
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section