Short Courses: CodeIgniter (PHP MVC Framework)

CodeIgniter Framework

CodeIgniter Framework

Take your websites to the next level with CodeIgniter MVC Framework in just 1 week. This course is totally practical and provides hands-on experience to the participants. The following are the contents of this course:

  • Advanced PHP concepts
  • Object Oriented PHP
  • Understanding the MVC Architecture
  • Server communication using AJAX

Students are expected to bring their own Laptops/Notebooks.

The fee for this course is PKR 2,800 and all the fee collected will be donated to Mishkat Welfare Trust
From: 30th June to 4th July 2014
Timings: 1:30 PM to 4:00 PM
Venue: Olives School, Judicial Colony, Rawalpindi.

Limited transport facility available for female students. Please mention it in the Comments field and provide your address.

At least 5 students must be enrolled for the course to commence.

Web Development using PHP

PHP

Build Data Driven Websites such as Flickr, Instagram, Twitter using PHP in just 1 week. This course is totally practical and provides hands-on experience to the participants. The following are the contents of this course:

  • Basics of PHP
  • Session Management
  • Storing data on client side using Cookies
  • Database Access using PHP
  • Email Generation
  • Using Management

Students are expected to bring their own Laptops/Notebooks.

The fee for this course is PKR 2,800 and all the fee collected will be donated to Mishkat Welfare Trust
From: 23rd June to 27th June 2014 25th June to 29th June 2014
Timings: 3:00 PM to 5:30 PM
Venue: Olives School, Judicial Colony, Rawalpindi.

Limited transport facility available for female students. Please mention it in the Comments field and provide your address.

Browser Back Button and Data Caching

By default, the web browser will always cache the data and when a user clicks on the browser Back button, the previously cached data will be loaded instead of generating a fresh request. While this is acceptable in most of the cases, some applications may not want this functionality. For example, a one-time form that has already been submitted etc.

All that is required is a set of instructions for the web browser. I have presented here three ways and any approach will be enough.

HTML

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate, post-check=0, pre-check=0" />
<meta http-equiv="Pragma" content="no-cache" />

PHP

header("Cache-Control: no-cache, no-store, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");

CodeIgniter

$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache"); 

This will prevent your browser from caching the page.

Use a Single PHP Session across Multiple Subdomains

By default PHP Sessions are created per domain / sub-domain and are limited within this scope. However there are ways by which a single PHP session will be available in multiple sub-domains.

Our Scenario

Suppose we have a domain named website.com with the following sub-domains:
http://www.website.com
image.website.com
video.website.com etc.

All the sub-domains share the same user accounts. It would be annoying to ask the user to re-enter the login details every time he/she visits a different sub-domains. For example, Google just takes the login credentials once and then we are automatically logged in the accounts.

The Solution

Any two of the following approaches can be used:

  1. Changing the php.ini file
  2. Just by updating the php.ini file, the changes will take effect without doing anything else in the coding. This file can be found in /etc in Redhat/CentOS or Fedora and in /etc/php5/apache2 in Ubuntu and Debian. After opening php.ini file, locate the line with the text session.cookie_domain = and modify it as below:

    session.cookie_domain=.website.com

    The dot (.) is very important to sub-domain access. Save the file reload it using the command:

    For Redhat/CentOS/Fedora (as root)

    service httpd reload

    For Ubuntu/Debian

    sudo service apache2 reload

    This will effect the whole system and that’s not what you would like to see if you are running multiple sites with difference domains on the same server.

  3. Providing the Session Cookie parameters at run-time
  4. Just by calling the session_set_cookie_params method at the top before the session_start. But this approach requires that this should be written on top of every page on every sub-domain. Either of the following will serve the purpose:

    session_set_cookie_params(0, '/', '.website.com');

    or

    ini_set('session.cookie_domain', '.website.com'); 

Try it and it should work!

Enabling Clean URLs for CodeIgniter

CodeIgniter by default will have to first load the index.php page which would then further load the rest of the pages. This can be removed from the URL simply by following these steps:

  1. Create a new file in the CodeIgniter root folder named .htaccess with the following contents.
    <IfModule mod_rewrite.c="">
        RewriteEngine On
        RewriteBase /
        #Removes access to the system folder by users.
        #Additionally this will allow you to create a System.php controller,
        #previously this would not have been possible.
        #'system' can be replaced if you have renamed your system folder.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
    
        #When your application folder isn't in the system folder
        #This snippet prevents user access to the application folder
        #Submitted by: Fabdrol
        #Rename 'application' to your applications folder name.
        RewriteCond %{REQUEST_URI} ^application.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
    
        #Checks to see if the user is attempting to access a valid file,
        #such as an image or css document, if this isn't true it sends the
        #request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c="">
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        # Submitted by: ElliotHaughin
    
        ErrorDocument 404 /index.php
    </IfModule>
    

    If CodeIgniter is not the webroot and is a subfolder then alter the line RewriteBase / to RewriteBase /[Sub Folder Name]/ where [Sub Folder Name] should be replaced by the actual name of the folder.

    Note: In case the above file doesn’t work, remove the <IfModule> opening and closing tags to test if for some reason Apache wasn’t able to load the module.

    Save the file.

  2. Open the file inside the CodeIgniter folder in the path application/config/config.php and alter the following line
    $config['index_page'] = 'index.php';

    Remove the ‘index.php’ to (empty string) and save.

  3. Enable the mod_rewrite module of Apache using the following command on Ubuntu:
    sudo a2enmod rewrite

    The Apache needs to be restarted before changes can take affect.

  4. Make sure the required settings are made in the sites file. In Ubuntu this file can be found at /etc/apache/sites-available/default and make the following changes:
    <Directory>
           ...
           Options FollowSymLinks
           AllowOverride FileInfo
           ...
    </Directory>
  5. Restart Apache using the following command:
    sudo service apache2 restart

Now test the website by using the URL without the index.php word and it should work fine.

Installing PHP 5.3 on IIS 6 and SQL Server

Things you need before Installation

This post assumes that you have that IIS 6.0 running on Windows 2003. In case you need assistance, refer to this website Installing IIS 6.0. Before getting started, we need the following tools:

Installation

  1. Install both FastCGI and Visual C++ 2008 Package.
  2. Unzip and extract PHP to C:\php (For the rest of the post, I will refer to it as the PHP directory)
  3. Inside the PHP directory you will find a file named php-production.ini, create a copy of this file and rename it to php.ini
  4. Open the php.ini to make the following changes. Uncomment the lines by removing the semicolon (;) and update the values.
    extension_dir = "C:\php\ext"
    error_log="C:\inetpub\temp\php-errors.log"
    cgi.force_redirect = 0
    fastcgi.impersonate = 1
    fastcgi.logging = 0
    cgi.fix_pathinfo=1
  5. Now to configure FastCGI, Open the command prompt and move to the folder C:\Windows\System32\inetsrv
  6. Execute the following three commands:
    cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP\php-cgi.exe"
    cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:10000
    cscript fcgiconfig.js -set -section:"PHP" -EnvironmentVars:PHP_FCGI_MAX_REQUESTS:10000
  7. Restart IIS and test if PHP is running

Configure PHP to Send Mails using External SMTP Server

Sending e-mails from website is a very task and almost all proper web applications have a need to send e-mails. This feature is available in PHP but by default PHP is configured to use the local SMTP server. However, this is not the only option you have. PHP can be configured to use an external SMTP server like Gmail for this purpose.

For that these additional packages will be required:

sudo apt-get install php-pear php-mail php-net-socket php-net-smtp php-mail-mime

Once the required packages have been installed, you can start sending the mails. Here is a sample PHP script I have created for the demo purpose:

<?php
   include("Mail.php"); // Actual path /usr/share/php/Mail.php
   include("Mail/mime.php"); // Actual path /usr/share/Mail/mime.php

   /* mail setup recipients, subject etc */
   $recipients = "destination@gmail.com";
   $headers["From"] = "youremail@gmail.com";
   $headers["To"] = "destination@gmail.com";
   $headers["Subject"] = "Mail Test (PEAR) 3";
   // Use this for Unicode messages
   $headers["Content-type"] = 'text/html; charset="UTF-8"';
  
   $mailmsg = "<div style='color:blue;'>Sample New Message<br />احمد</div>";
      
   $mime= new Mail_mime("\n");
   $mime->setHTMLBody($mailmsg);
      
   $mailmsg = $mime->get();
      
   $headers = $mime->headers($headers);
     
   /* SMTP server name, port, user/passwd */
   $smtpinfo["host"] = "smtp.gmail.com";
   $smtpinfo["port"] = "25";
   $smtpinfo["auth"] = true;
   $smtpinfo["username"] = "youremail@gmail.com";
   $smtpinfo["password"] = "";
   
   /* Create the mail object using the Mail::factory method */
   $mail_object =& Mail::factory("smtp", $smtpinfo);
   /* Ok send mail */
   $mail_object->send($recipients, $headers, $mailmsg);
?>

Load the page in the browser and you will be happy to see that it works! 🙂