Skip to main content
#! code

Main navigation

  • Tools
  • Snippets
  • About
  • Contact
  • Services
  • Support Us!
PHP Logo

PHP Password Generator

8th March 2008 - 2 minutes read time

Here is a very simple function that will generate a string of random characters, ideal if you want to create a password for a new user.

function generatePassword($length=10)
{
 $pass = '';
 $parts = array_merge(range(0, 9),range('a', 'z'),range('A', 'Z'));	
 for ($i=0; strlen($pass) <= $length; $i++) {
  $pass .= $parts[array_rand($parts)];
 }
 return $pass;
}

Use the function like this.

echo generatePassword();

To create a longer password string just pass a parameter with the function.

echo generatePassword(5);

 

WordPress Logo

Changing Your Wordpress Database Prefix

7th March 2008 - 5 minutes read time

A good security tip when installing your Wordpress blog is to change the database table prefix, the idea is that this will hide the tables from any hackers looking to compromise your blog. This can be done in the wp-config.php file and the variable $table_prefix. Changing this value from the default wp_ to, for example, blog_ will change the table wp_posts to blog_posts, making it more difficult for hackers to find it. Using blog_ is only an example, you should treat the prefix like a password, include letters and numbers to make it more difficult to find.

But what happens if you have installed your system and want to change the prefix? Well you need to change the $table_prefix variable in your wp-config.php file and alter the tables in your Wordpress database. Which one you do first is up to you, but you should go them both as fast as you can. Here are some MySQL statements that should help you to do this.

PHP Logo

Extract Links From A HTML File With PHP

6th March 2008 - 2 minutes read time

Use the following function to extract all of the links from a HTML string.

function linkExtractor($html)
{
 $linkArray = array();
 if(preg_match_all('/<a\s+.*?href=[\"\']?([^\"\' >]*)[\"\']?[^>]*>(.*?)<\/a>/i', $html, $matches, PREG_SET_ORDER)){
  foreach ($matches as $match) {
   array_push($linkArray, array($match[1], $match[2]));
  }
 }
 return $linkArray;
}

To use it just read a web page or file into a string, and pass that string to the function. The following example reads a web page using the PHP CURL functions and then passes the result into the function to retrieve the links.

Apache HTTP Logo

Using mod_rewrite On Form Parameters

5th March 2008 - 6 minutes read time

Using mod_rewrite on websites is fairly straightforward and can create some lovely looking URL structures. Instead of having a URL that contains lots of odd looking parameters like this:

http://www.example.com/example.php?parameter1=value1&parameter2=value2

You can use a .htaccess file to rewrite the URL on the server side in order to shorten this to something like this:

http://www.example.com/p-value1

In this occasion the value of parameter2 will always be value2 so we can just include that in the rewrite rule, which would look something like the following. $1 is a back-reference to the first parenthesized value matched in the RewriteRule.

PHP Logo

Simple PHP Script To Hide An Email Address In An Image

4th March 2008 - 3 minutes read time

Spam is a problem. You want to allow people who genuinely want to get in touch to see your email address, but doing this invariably leads to you getting thousands of spam emails.

One solution is to hide your email address in an image, but it can be a pain to create an image for every email address you need. A better solution is to use the PHP GD functions to create an image at runtime so that your email address is displayed, but is completely unreadable to spammers.

To to this you will need to create an image tag on your website, here is an example.

PHP Logo

Getting All Permutations Of An Array In PHP

29th February 2008 - 3 minutes read time

Here are two ways in which you can figure out all of the different permutations of an array.

The first is using a recursive algorithm. This nibbles apart the array and sticks it back together again, eventually resulting in all of the different permutations available.

Create A Simple Flash Presentation

28th February 2008 - 4 minutes read time

One common thing that can be done with Flash is to create presentations. These presentations can then be exported to a swf file and viewed by just about everybody.

To make a presentation create a new flash document and create a new layer. This layer will be used to keep the next and previous buttons in a consistent place.

On the first frame add the following code to stop the animation running at the first frame.

stop();

Next, create a button that you can use for moving forward and backward through the presentation. This can be done by drawing an object on the stage, right clicking on it and selecting Convert to Symbol.... You can then add these buttons to the stage and create events for them.

For the previous button a function exists in ActionScript called prevFrame(). So all you need to do is attach that function call to the release event (ie. after a user has clicked on it) on that button.

Apache HTTP Logo

Apache Bench Tool

25th February 2008 - 5 minutes read time

The Apache Bench tool can be found in the bin directory of any standard instillation of the Apache HTTP server. It can be used to test the response times of any web server you want and can be useful if you want to stress test a mission critical server before it goes live.

To use the tool open a command prompt (or terminal), navigate the Apache bin folder and find the program ab, this is the Apache Bench tool. The simplest form of running the tool is to use a single URL. However, you must enter a full file name or the tool will give you an invalid URL error.

ab http://www.google.com/index.html

This gives the following output.

Apache HTTP Logo

Avoiding URL Canonicalisation With mod_rewrite And Apache

22nd February 2008 - 3 minutes read time

URL canonicalisation is where you have a website with different URLs outputting the same content. When search engine spiders see all this content that is the same they can get confused as to what page to display in search engine result pages. The following URLs, although they are different, actually produce the same content.

http://www.example.com
http://example.com
http://www.example.com/
http://www.example.com/index.html

The way to solve this issue is to redirect any requests to a single page using mod_rewrite. Add a .htaccess file to your root directory and include the following line to turn on the engine.

RewriteEngine On

The following rule will redirect the www page to the non-www page.

WordPress Logo

Creating A Widget Proof Wordpress Theme

21st February 2008 - 5 minutes read time

Wordpress widgets are a way to customise the sidebar of your blog very easily and where included with the default Wordpress instillation from version 2.2 onwards. With a widgetised theme all you need to do to change the menu system on your blog is drag and drop features and edit some simple parameters like heading.

To include widgets on your blog you need a widget ready Wordpress theme. However, this isn't as easy as it sounds because only a small section of themes are widget enabled.

To make a widget enabled theme you can use any existing theme and just a few lines of code. First off, find the file called sidebar.php in your Wordpress theme directory. You might not have this file, but you are looking for the section of code that displays the navigation menu.

Pagination

  • First page First
  • Previous page ‹‹
  • …
  • Page 74
  • Page 75
  • Page 76
  • Page 77
  • Current page 78
  • Page 79
  • Page 80
  • Page 81
  • Page 82
  • …
  • Next page ››
  • Last page Last

Categories

  • Ansible
  • Apache
  • Book Reviews
  • CSS
  • DOS/Windows
  • Docker
  • Drupal
    • Drupal 7
    • Drupal 8
    • Drupal 9
    • Drupal 10
    • SimpleTest
  • Flex/Flash
  • General
  • Git
  • Godot
  • HTML/XHTML
  • JavaScript
    • JavaScript Strings
    • JavaScript Websites
    • JQuery
    • MooTools
    • OpenLayers
    • Script.aculo.us
  • Linux/Unix
  • OSX
  • PHP
    • Phing
    • PHP Arrays
    • PHP Questions
    • PHP Strings
    • PHP Websites
    • Zend Framework
  • Python
  • Regular Expressions
  • SQL
    • MS SQL
    • MySQL
    • PostgreSQL
  • Vagrant
  • Websites
  • WordPress

Footer Social

  • Mastodon
  • Github
  • Drupal
  • Facebook

Footer

  • About
  • Colophon
  • Privacy Policy
  • Support Us
  • Terms And Licence

© 2023 #! code
Hash Bang Code Ltd.
Company Registration No 13867421