PHP

How to Find Your php.ini File

Tags:

When overriding your php.ini file, it helps to know where your server's current php.ini file is. Just run the following command in the terminal to locate your php.ini file:

10 Free Open Source PHP/MySQL Content Management Systems

Tags:

Many Web designers begin with WYSIWYG HTML editors like Dreamweaver or Frontpage, not realizing that there are easier ways to get things done — like using a free content management system!

Here are 10 top-notch open source content management systems that are powered by PHP/MySQL — in no particular order, except that I am biased towards Drupal ;)


How to Fix Gallery 2 for SEO

Tags:

Gallery 2 is a great photo gallery script. It has some SEO issues though.

How to Run Your Favorite Programs at Work Without Admin Access

Many people have probably experienced this problem — you are at work and want to use your favorite programs, but you don't have the admin access that you need to install software.

There is a way to run software on Windows without needing to install anything. It's the perfect solution for restrictive work environments and public Internet cafes.

How to Bulk 301 Redirect in PHP

I usually do my redirects with an .htaccess file, but it is also possible to do redirects with PHP. A quick example is shown below:

<?php
// Create a function to send the correct headers
function redirect($url) {
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: $url");
   exit();
}

// Get the path to the file that was requested
$filename = $_SERVER['REQUEST_URI'];

// Perform the redirects
switch ($filename) {
case "/old-page1.html":
   redirect("http://www.example.com/new-page1");
   break;
case "/old-page2.html":
   redirect("http://www.example.com/new-page2");
   break;
case "/old-page3.html":
   redirect("http://www.example.com/new-page3");
   break;
}
?>

One reason you might do this is if the .htaccess redirects are not working and you need immediate results.

Tips for success:

  1. Make sure that there is no whitespace or content before the opening PHP tag, and that nothing is sent to the browser before the headers.
  2. Check your redirects with the Firefox liveHTTPheaders extension to make sure the correct headers are being sent.

Update: 301 OK Error

[Update, December 26, 2006]

I added a 301 redirect to a site with PHP today and get an error when I checked the headers. The page redirected, but was sending a header that said "301 OK" instead of the correct "301 Moved Permanently". Search engines may not obey an incorrectly sent header.

The code below redirects the /index.php page to the correct home page URL, which is http://www.example.com/. In this case, Google had indexed both URLs and I needed to redirect everything to a single home page URL. See Google engineer Matt Cutts' post on URL canonicalization for more information on the reasons for this kind of redirect.

Here is the original code, that was sending incorrect headers:

$homepage = 'http://www.example.com/';
$filename = $_SERVER['REQUEST_URI'];
if ($filename == '/index.php') {
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $homepage");
}

Here is the fixed code that sent the correct headers:

$homepage = 'http://www.example.com/';
$filename = $_SERVER['REQUEST_URI'];
if ($filename == '/index.php') {
    header("Status: 301 Moved Permanently");
    header("Location: $homepage");
}

See my post about PHP/Drupal 404 OK errors for more information.

Drupal PHP Snippet: How to Check if a Visitor is Logged In

Tags:

A common question when using Drupal is "How do I show something only to users that are not logged in?" (or vice versa). This is the simple answer for snippets of content:

<?php

// You must include this next line
global $user;

// if visitor is not logged in
if (!$user->uid) {
  // do something if not logged in
} else {
  // do something else if logged in
}

?>

I see many people searching this site for the answer to that. I hope someone finds it useful.

Drupal Theming Tutorials

Tags:

I've found some great Drupal templating tutorials online.

NickLewis.org has a series about Extreme Drupal Theming with PHPtemplate. Here is one tutorial from that series on how to customize the login form.

Bryght.com has a great introduction to converting a CSS/HTML design to a Drupal theme. Bryght.com also has an interesting tutorial on how to create a contact form in Drupal with the survey module. The survey module can be found here. I haven't tried that method of creating contact forms, but it looks interesting.

The drupal.org phptemplate docs are also very good. The PHPTemplate Theme Snippets section has useful Drupal template recipes.

Free Photo Gallery Software (PHP, AJAX, Ruby on Rails)

PHP-based Photo Gallery Software

Gallery

Gallery is the classic free PHP photo album program for web sites. If you don't have a database on your server you can use Gallery 1. Gallery version 2 requires a database. The Gallery web site has a full list of requirements. Gallery can also be integrated into several content management systems, including Drupal.

How to Make a Drupal Theme

Making a custom Drupal theme is actually quite easy. A Drupal theme is just a few PHP files and a CSS file. I prefer the PHPtemplate theme engine (the default one) but you have several choices. See the bottom of this post for a link to the official Drupal Theme Developer's Guide which has information on other Drupal template engines.

The following information was originally written for Drupal 4.7, but the concepts also work for Drupal 5 and 6 too.

Web Design Tips and Tutorials

I found an interesting list of web design links at devlisting.com.

Also check out the excellent reference page, the web developer's handbook, and the accompanying blog.

Syndicate content