Skip to main content


Showing posts from May, 2020

Search engine friendly URLs

If you want your website to have friendly URLs you can use this .htaccess code.  With this example code, the URL would cause the page about.htm to load. RewriteEngine On RewriteRule ^([^\./]+)$ /$1.htm [L] With the above example, if there is no dot or slash in the requested name, and the request is not empty (meaning the default page), it will take the request and put .htm on the end. Rewrites and https If you are using https, then you may need to make some changes to your .htaccess code.   If you are doing rewrites that use the full URL (for example and you will be switching between http and https, then you will need to add a RewriteCond to your code.   You will need to test for https and code the full URL accordingly (using http or https).   The following examples show how to test for https or the lack of https. RewriteCond %{HTTPS} on RewriteCond %{HTTPS} !=on Blocking traffic to your website To block an  IP address  or a  domain

Domain and subdomain pointing

Stop here!   If you want to do domain pointing, before you even start with htaccess code, make sure your pointed domain name resolves to your main domain name default page.  Many people think they have trouble with their htaccess code only to find the domain name is not properly pointed to their package in the first place.  So make sure your pointed domain name works, resolving to your default page, before going any further.  This is obviously not an issue if you are doing subdomain pointing. The examples below assume that your root public directory (where you would put this .htaccess file) is  htdocs , which is the default setup for Apache.   If your setup is different, then you may need to adjust the examples accordingly. This approach has you code the first part of the domain or subdomain name and the associated subdirectory in your .htaccess file.   For example: RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !^/ subdirectory / RewriteCond %{HTTP_HOST} ^(www\.)? name \

Auto Password Change and Email Notification

This script will change your .htpasswd password to a new random password (with encryption) and send an email to notify your users of the change.  You can run this script automatically using cron at what ever interval you like, or run it manually. - - Start Script Here - - <?php $username  =  "XXXXXXX" ;  // the username specified in the .htaccess file $length  =  "10" ;  // length of the password $emailaddress  =  "" ;  // email address that forwards to your mailing list // generate password $spec_charset  = array( "!" , "@" , "#" , "$" , "%" , "^" , "&" , "*" , "_" , "+" . "?" , "=" ); $chars  = array(); $pass  =  "" ; for ( $i  =  1 ;  $i  <=  $length ;  $i ++) {   for ( $i  =  48 ;  $i  <=  57 ;    $i ++) {  $chars [] =  chr ( $i ); }  // numbers    for ( $i  =  65 ;  $i  <=  90 ;   

Setting up password protection

The best way to secure content on your website is to use .htaccess/.htpasswd protection.   This will password protect any directory and all directories below.   You will need to create a .htaccess file which you put in the directory you want to protect.   You will also need to create a .htpasswd file which you will put out of reach (see tip on Securing Your Package).   The .htaccess file should contain the following: AuthUserFile /full_unix_path_to_your_file/.htpasswd AuthName "Any Name You Want" AuthType Basic require user username Where username is the name of the user specified in the .htpasswd file. You can also make that last line require valid-user to accept any user specified in the .htpasswd file. You can also limit the password protection. For example put the .htaccess code inside these tags <files file.ext> htaccess protection code goes here </files> to limit the password protection to just the file "file.ext". The .htpasswd file should genral

Cron Basics

Cron is used to execute a script or command at a specified interval or point in time.   The control file is a crontab file (named crontab - with no extension).   A crontab file is made up of one line per entry.   An example crontab entry looks like this: 0 * * * * php $HOME/cron.php >>$HOME/cron.log 2>>$HOME/cron.err In the above example, the script cron.php is executed every hour on the hour. An entry line contains nine fields, which must be separated by white space (tabs or spaces).  The fields are: Minute of the hour in which to run (0-59) Hour of the day in which to run (0-23) Day of the month (0-31) Month of the year in which to run (1-12) Day of the week in which to run (0-6) (0=Sunday) Optional shebang, if needed (for running php scripts in the above example, may vary depending on your host config) The path/command to execute (note that $HOME gets you to your user root) The log file - which begins with >> The error log file - which begins with 2>&

SQL coding examples

This SQL and PHP will count how many rows contain unique elements in your database.   For example, you may have a database of user entries and want to know how many entries each user has.   In this example, you want to know how many rows contain each unique element "name": $query = mysql_query("SELECT name, COUNT(*) AS number FROM $table GROUP BY name"); if ( mysql_num_rows($query) == 0 ) echo "No rows in the database"; while ($row = mysql_fetch_array($query)) {   $name = $row['name'] ;   $count = $row['number'] ;   echo "$name has $count rows in the database<br>"; } Using PHP/MySQL scripts at your host and at home If you have PHP/MySQL scripts that you want to use both at your host and on your home machine, you can add a few script lines that will allow you to do that without requiring any further modifications when you transfer your scripts back and forth.  In your MySQL configuration statements include a simple "

Tracking and Securing Downloads

If you want to report or track downloads from your website, try this script.   This script will send you an email every time you have a download.   The email will tell you what file was downloaded and who did the download.   You could change this script to keep counts (store them in flat file or MySQL) if you desire. The variable $directory is the directory where the download files are located.   If you want the script in the same directory as the files then use "./" as the directory (you must always have the slash). In your html page, use the following structure as your download link (where name.txt is the file name to download): <a href="download.php?file=name.txt">download</a> Then you use the following script (called download.php): - - Start Script Here - - <?php $emailaddress  =  "" ; $filename  =  $_GET [ 'file' ]; $directory  =  "downloads/" ; $path  =  "$directory$filename" ; putenv (

Form Mail

Here is a simple Form Mail script.   It can be used on your website for people sending you messages, rather than using an email link.   The script provides a form with name, email address, and message.   It validates that all three fields are completed and that the email address format is valid.   It also provides some protection against mail bombing by only allowing 3 messages to be sent from a single browser session.   You can easily modify the script to fit the theme of your site, or change the form.   The example below uses the script name mail.php (see the form action=), so put the script in a file by that name. - - Start Script Here - - <?php if (isset( $_POST [ 'send' ])) {    session_start ();    $name  =  $_POST [ 'name' ];    $email  =  $_POST [ 'email' ];    $content  =  $_POST [ 'content' ];   if (! preg_match ( "(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i" ,  $email ))  $alert  =  "You have entered an invalid email address.&qu