Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Want to have a style switcher that lets your site’s visitors choose a different style sheet? Want it to work even if there is no JavaScript support? The trick is to use a server-side language like PHP, which is what I use for my style switcher.
Using PHP to let the user switch to a different CSS file is nothing new. But it is one of the things that I am often asked about, so I thought it would be good to have a write-up to refer people to in the future.

As I mentioned this will work when JavaScript is disabled. It will not work when cookies are disabled, however. Nothing bad will happen, but it simply won’t work and the user will be returned to the same page they were on. Just to make you aware of that right from the start.

I use this style switcher to let visitors switch between two different layouts (“Zoom” and “Normal”) here on 456 Berea Street. If you want to let the user choose between more than two stylesheets you will need to come up with a different solution.

I wouldn’t call myself a PHP expert by any means, so if you think my code could be improved in any way, please let me know.

To switch styles, all you need to do is activate the link to the styleswitcher. As you may have noticed if you have tried that, the URL that invokes the styleswitcher is always the same: /styleswitch/. The link leads to a php file that looks like this:

  1. <?php
  2. $layout = (isset($_COOKIE['layout']) && ($_COOKIE['layout'] == "zoom")) ? "main" : "zoom";
  3. setcookie("layout", $layout, time()+31536000, '/');
  4. $ref = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : "http://{$_SERVER['SERVER_NAME']}/";
  5. header("Location: $ref");
  6. ?>
Here’s what happens, line by line:

  • Line 2: If the layout cookie exists (the browser has been here before) and has the value “zoom” (the user has switched to Zoom layout), $layout is set to “main”. Otherwise $layout is set to “zoom” (the browser doesn’t accept cookies, the style switcher has not been used with this browser, or the user has switched to Normal layout). Note that “main” and “zoom” are the file names (except for the .css extension) used for the respective styles. Change these values to match your file names.
  • Line 3: The value of $layout is stored in a cookie.
  • Line 4: If a referer header exists, its value is stored in $ref. If it doesn’t exist, $ref is set to “http://{$_SERVER[‘SERVER_NAME’]}/” as a backup to redirect the browser to the home page.
  • Line 5: The browser is redirected to the URL stored in $ref.
On all pages a check is performed to decide which stylesheet to load:

  1. <?php
  2. $layout = (isset($_COOKIE['layout']) && ($_COOKIE['layout'] == "zoom")) ? "zoom" : "main";
  3. ?>
If a cookie exists and its value is “zoom”, $layout is set to “zoom”. Otherwise, $layout is set to “main”. At this point $layout contains the name of the CSS file that should be loaded.

Finally, when loading the main style sheet the value stored in $layout is inserted instead of the file name in the @import path:

  1. <style type="text/css" media="screen,projection">
  2. @import '/css/<?php echo $layout; ?>.css';
  3. </style>
And that’s it. Use it, spread it, improve it.
Thursday, September 17, 2009

Useful & Standalone PHP Classes By Symfony

Symfony, one of the most popular PHP frameworks around, is sharing several standalone PHP classes (under the Symfony Components website) which can ease the job of a PHP developer.
They don’t require Symfony to be used & can be used with any PHP code.

Currently, 3 classes are ready to be used:

YAML
It parses YAML strings and converts them to PHP arrays. It can also converts PHP arrays to YAML strings.
Event Dispatcher
It’s a good way to make your code more flexible. It’s also a great way to make your code easily extensible by others.
Symfony Event Dispatcher is a PHP library that provides a lightweight implementation of the Observer design pattern.
Dependency Injection
It provides a lightweight and robust Dependency Injection Container for classes that implements the Dependency Injection pattern.

And, 2 new classes are on the way:

  • Templating
  • Request Handler
All the code provided is very well-documented & totally free to use.

Requirements: PHP
Website: http://components.symfony-project.org/
Wednesday, May 6, 2009

Free PHP Uptime Monitor Script: phpWatch

As we all experience, every hosting account or server sometimes fails, no matter how good the hosting company is. And, this can be accepted as normal when it is within the limits.
For better reacting to the downtimes, analyzing the health of the web application & the environment it is hosted on, uptime monitoring is a working solution.



phpWatch is a free PHP uptime monitoring script that can watch unlimited number of websites and send notifications via e-mail or SMS (for US phones).
With an Ajaxed interface, new monitors & notifications can be easily configured. And, once the cron job / scheduled task -that pings the servers in desired time intervals- is set, the script is ready to go.
phpWatch also offers an API which lets other applications to query the monitored services, get the statistics and use the notification system for sending SMS and e-mail alerts.

Requirements: PHP, MySQL
Website: http://aaron-rosenfeld.com/2008/08/28/phpwatch-relea...
Demo: http://aaron-rosenfeld.com/phpWatch/demo
Sunday, April 26, 2009

Analyzing PHP Performance: PHP Quick Profile

Firebug is the most popular tool for analyzing HTML, CSS & JavaScript based webpages. Now, there is PHP Quick Profiler (PQP), which can be called as Firebug for PHP.
It is specially focused on analyzing the performance of PHP codes. With a beautiful interface, PQP can:
  • log memory usage
  • display the number & sizes of included files
  • show page execution times
  • log database queries

PHP Quick Profiler is not a plug & play solution as every project may have a totally different structure. But, by following the setup information & the example provided, it is easy to integrate.
To see how functional it is, you can check the demo.

Requirements: No Requirements
Website: http://particletree.com/features/php-quick-profiler/
Demo: http://particletree.com/examples/pqp/
Saturday, October 18, 2008

Automated Reflected Images With PHP

Easy Reflections, a PHP script that enables you to create reflections (Web 2.0 look) of any image.
Supported image types are GIF, JPG & PNG.



There is a built-in caching mechanism to prevent creating same images again & again.
And, fade levels used can be configured.
The script works with both PHP4 & PHP5 and requires GD extension 2.0.1+ installed.

Requirements: GD extension 2.0.1+
Compatibility: PHP4 & PHP5
Website: http://reflection.corephp.co.uk/
Saturday, September 13, 2008

PHP Smart Image Resizer Script

Smart image resizer is a PHP script which makes it very easy to resize & crop images. And, this is done without touching the original image.
The width & height of the image to be resized is mentioned in the tag like:
<img src="/image.php/coffee.jpg?width=200&height=200ℑ=//uploads/coffee.jpg" alt="Coffee" />
and image is resized via the GD library.
Newly created image is stored in a cache directory & won’t be re-created until new dimensions are mentioned to improve the system performance.
Cropping an image also warks the same way. Simply mention the crop ratio & that’s all.
Smart Image Resizer supports JPG, GIF & PNG images and requires PHP 5.1.0+ to run.

Requirements: PHP 5.1.0+, GD library
Compatibility: JPG, GIF, PNG
Website: http://shiftingpixel.com/2008/03/03/smart-image-resi...
Thursday, July 17, 2008

Free Survey Application: AskPeople

AskPeople is a free PHP survey application that is simple but functional.

It supports most of the entry options with checkboxes, radio buttons, textboxes, textareas & dropdowns. Using AskPeople, a complicated survey can be created in minutes.

AskPeople has a template system where a new design can be implemented for the surveys to match the look of your website.

Some features of this free PHP survey application:

  • Secure, multi-user admin interface.
  • Unlimited, completely flexible templates.
  • Built in file manager.
  • Multipage surveys using pagebreaks between groups of questions.
  • Make any question ‘required’ to ensure respondents complete it.
  • Optional cookie-based protection against repeated submission of forms.
  • Plain-text questionnaire export.
  • Raw data editor.
  • One-click project backup.

This is a handy application for creating & managing a job application form or making a market research & more.


Requirements: PHP 4+
Website: http://www.askpeople.co.uk/
Demo: http://www.askpeople.co.uk/demo
Download: http://www.askpeople.co.uk/download/index.php
Saturday, July 12, 2008

PHP - Ajax Chat Application: Mia Chat

Mia Chat is an Ajaxed PHP chat application which is very simple to use & install.

It functions similar to the messenger applications, you can add buddies, chat with several users at the same time, change your online status & more.

This PHP chat supports many databases for storing data like MySQL, MSSQL, Oracle 8+, MS SQL Server 2000+, SQLite 2+ and PostgreSQL 7+.


Requirements: PHP 5.0.5+, Apache Server 1.3+
Compatibility: All Major Browsers & Databases
Website: http://wiki.brilaps.com/wikka.php?wakka=MiaChat
Demo: http://mia.brilaps.com/

Thursday, June 26, 2008

Web-Based PHP FTP Application: phpWebFtp

phpWebFtp is an open source web-based FTP application built with PHP.

It is not a simple file manager as it can be installed anywhere and connects to any FTP server.

You can configure the application from a config file. Some configuration options include: maxFileSize, resumeDownload & defaultServer.

Features of this PHP FTP Application:

  • Built-in free 22 language packs
  • Easy Binary/ASCII mode switching
  • WYSIWYG file editor for .html, .htm files
  • Built-in file or directory download mode
  • Special .zip files extraction to the server feature
  • CHMOD function for applying permissions to the files and folder
  • UNZIPing mode for extracting zipped files on the server
Requirements: PHP
Compatibility: All Major Browsers
Website: http://www.phpwebftp.com
Download: http://www.phpwebftp.com/download/