Friday, September 6, 2013

PHP image output and browser caching

The code is pretty simple, and borrows heavily from code pasted by mandor at mandor dot net for the PHP header function. We generalised it for different graphic file types and a second function so it works when PHP is an Apache module or cgi.
The first function displayGraphicFile is to return a graphic file. The function does assume the file exists.
// Return the requested graphic file to the browser
// or a 304 code to use the cached browser copy
function displayGraphicFile ($graphicFileName, $fileType='jpeg') {
  $fileModTime = filemtime($graphicFileName);
  // Getting headers sent by the client.
  $headers = getRequestHeaders();
  // Checking if the client is validating his cache and if it is current.
  if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $fileModTime)) {

    // Client's cache IS current, so we just respond '304 Not Modified'.
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 304);
  } else {
    // Image not cached or cache outdated, we respond '200 OK' and output the image.
    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $fileModTime).' GMT', true, 200);
    header('Content-type: image/'.$fileType);
    header('Content-transfer-encoding: binary');
    header('Content-length: '.filesize($graphicFileName));
    readfile($graphicFileName);
  }
}
The second function to get the header request details. We specifically require the ‘If-Modified-Since’ header.
// return the browser request header
// use built in apache ftn when PHP built as module,
// or query $_SERVER when cgi
function getRequestHeaders() {
  if (function_exists("apache_request_headers")) {
    if($headers = apache_request_headers()) {
      return $headers;

    }
  }
  $headers = array();
  // Grab the IF_MODIFIED_SINCE header
  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
    $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
  }
  return $headers;
}

Apache Access Log Rotation In AppServ And Xampp

Apache access log rotation in AppServ and Xampp is disable by default. By default,access log will be created in directory path “xampp/apache/logs/access.log” for Xampp and “AppServ/ApacheX.X/logs/access.log” for AppServ. On a busy server, access log will be grows very fast equal to high traffic for apache server. Without rotation, access.log will be very large. Manual rotation by periodically backup existing log cannot be done while apache server is running. In this case, apache continues writing to access.log until the server is stopped.
Small file log will be lighten work load of apache server, because it’s need more resource to handle writing to a big file. In this tutorial, access.log will be rotate daily base, so the name of log will be “access_year-month-date.log” Ex: access_12-01-01.log for access log at January 1, 2012.

Editing httpd.conf configuration

Edit your httpd.conf in this path “Xampp/apache/conf/httpd.conf” for Xampp and “AppServ/ApacheX.X/conf/httpd.conf” for AppServ. Find the line contains config like this:
1CustomLog "logs/access.log" common
Comment this line by adding # in the beginning of line. Then, add new line bellow old config and fill with this config:
1CustomLog "|bin/rotatelogs.exe logs/access_%y-%m-%d.log 86400" common
Make sure that rotatelogs.exe exist in bin directory. To tell apache should rotate log daily base, we should add numeric value 86400 that means interval in seconds after new file is generated. Number 86400 came from (24 hours * 60 Minutes * 60 Seconds) that means apache log should rotate daily. Don’t forget to savehttpd.conf and restart your apache. After the server up, see at directory path “xampp/apache/logs” for Xampp and “AppServ/ApacheX.X/logs” for AppServ, there is access log with new format name: “access_year-month-date.log”. For more explanation, see Apache HTTP Server Log Files.

Combined Log Format

Another commonly used format string is called the Combined Log Format. It can be used as follows.
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
CustomLog log/access_log combined
This format is exactly the same as the Common Log Format, with the addition of two more fields. Each of the additional fields uses the percent-directive %{header}i, where header can be any HTTP request header. The access log under this format will look like:
127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 "http://www.example.com/start.html" "Mozilla/4.08 [en] (Win98; I ;Nav)"
The additional fields are:
"http://www.example.com/start.html" (\"%{Referer}i\")
The "Referer" (sic) HTTP request header. This gives the site that the client reports having been referred from. (This should be the page that links to or includes/apache_pb.gif).
"Mozilla/4.08 [en] (Win98; I ;Nav)" (\"%{User-agent}i\")
The User-Agent HTTP request header. This is the identifying information that the client browser reports about itself.


Thursday, September 5, 2013

Setup Remote Access Key cPanel Server WHM

Setup Remote Access Key

For WHM version 11.36 
(Home >> Cluster/Remote Access >> Setup Remote Access Key
This feature can be used to generate an access key. This access key can be used for automatic account creation scripts, external billing software, and allowing servers in your DNS cluster to exchange records. 
To generate a new access key: 
  1. Login to your_cpanel_server.com:2087 (example)
  2. Go to: Home>>Cluster/Remote Access>>Setup Remote Access Key
  3. Click Generate New Key.
  4. Copy and paste the new access key to the appropriate file on the remote server(s) that need to communicate with your web server. To use the remote access key to configure a DNS server cluster, follow our steps for configuring a cluster, pasting the key in on the Create Trust Relationshipscreen.

ACCESS HASH AUTHENTICATION

You can write your script so that it includes an access hash, or "key," in the HTTP header that it sends to the server when it calls the API function. This method is only available to WHM users. 
The access hash can be set up, and subsequently accessed, from the WHM Setup Remote Access Key feature. On your server, the hash resides in/root/.accesshash.

Install Jboss 5.1.0.GA to Windows 7

Today I am going to show you how to install Jboss 5.1.0.GA on  a Windows 7 machine.
1. First of all download the binaries from http://www.jboss.org/jbossas/downloads/
2. The downloaded file is a zip is named jboss-5.1.0.GA-jdk6.zip. As you can see it is a zip archive and not an executable.
3. Create an installation folder (I created one at C:\Program Files\Jboss) and extract the archive there.
4. Add the path to JBoss directory to JBOSS_HOME enviroment variable (picture 1). For me it is C:\Program Files\Jboss\jboss-5.1.0.GA








5. Add the path to JDK directory to JAVA_HOME enviroment variable (picture 1). For me it is C:\Program Files\Java\jdk1.6.0_31
6. Gongratulations! We are ready! Now to start the application server open a shell window using WinKey + R then type cmd and hit Enter.
7. Type
C:\Program Files\Jboss\jboss-5.1.0.GA\bin\run.bat  -b 0.0.0.0
Notice the parameter -b! This parameter assures that server isvisible from all computers inside the network.
7. If you want to see if the application server has started correctly, inspect the log output in the shell will it is loading. When the process is finished open a browser and type: http://localhost:8080/web-console/

Heap size for JVM - Invalid initial heap size

Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mistakes made by developers while using them:
  1. Missing m, M, g or G at the end (they are case insensitive). For example,

    java -Xmx128 BigApp
    java.lang.OutOfMemoryError: Java heap space
    
    The correct command should be: java -Xmx128m BigApp. To be precise, -Xmx128 is a valid setting for very small apps, like HelloWorld. But in real life, I guess you really mean -Xmx128m
  2. Extra space in JVM options, or incorrectly use =. For example,

    java -Xmx 128m BigApp
    Invalid maximum heap size: -Xmx
    Could not create the Java virtual machine.
    
    java -Xmx=512m HelloWorld
    Invalid maximum heap size: -Xmx=512m
    Could not create the Java virtual machine.
    
    The correct command should be java -Xmx128m BigApp, with no whitespace nor =. -X options are different than -Dkey=value system properties, where = is used.
  3. Only setting -Xms JVM option and its value is greater than the default maximum heap size, which is 64m. The default minimum heap size seems to be 0. For example,

    java -Xms128m BigApp
    Error occurred during initialization of VM
    Incompatible initial and maximum heap sizes specified
    
    The correct command should be java -Xms128m -Xmx128m BigApp. Its a good idea to set the minimum and maximum heap size to the same value. In any case, dont let the minimum heap size exceed the maximum heap size.
  4. Heap size is larger than your computers physical memory. For example,

    java -Xmx2g BigApp
    Error occurred during initialization of VM
    Could not reserve enough space for object heap
    Could not create the Java virtual machine.
    
    The fix is to make it lower than the physical memory: java -Xmx1g BigApp
  5. Incorrectly use mb as the unit, where m or M should be used instead.

    java -Xms256mb -Xmx256mb BigApp
    Invalid initial heap size: -Xms256mb
    Could not create the Java virtual machine.
http://avdheshsemwal.blogspot.com/2013/02/heap-size-for-jvm.html

Wednesday, September 4, 2013

Concat two different fields in cakephp in find statement

<?php
$this
->ModelName->virtualFields = array(
    
'full_name' => "CONCAT(ModelName.first_name, ' ', ModelName.last_name)"

);
$list $this->ModelName->find("all", array(
    
"fields" => array(
        
"ModelName.id",
        
"ModelName.full_name"
    
)
));

?>

Concatenate/concat/group_concat two columns/rows with MySQL query

Two/more rows concatenate-
You can use GROUP_CONCAT.
As in:
select person_id, group_concat(hobbies separator ', ')
    from peoples_hobbies group by person_id;
Death: As Dag stated in his comment, there is a 1024 byte limit on result. to solve this run this query before your query:
set group_concat_max_len=2048
Off course, you can change 2048 accourding to your needs.

Two/more columns concatenate-
You can use the CONCAT function like this:
SELECT CONCAT(SUBJECT, ' ', YEAR) FROM table