Tuesday, July 23, 2013

How to get exact browser name and version php

<?phpfunction getBrowser()
{
    
$u_agent  $_SERVER['HTTP_USER_AGENT'];
    
$bname    'Unknown';
    
$platform 'Unknown';
    
$version  "";
  
    
//First get the platform?
    
if (preg_match('/linux/i'$u_agent)) {
        
$platform 'linux';
    } elseif (
preg_match('/macintosh|mac os x/i'$u_agent)) {
        
$platform 'mac';
    } elseif (
preg_match('/windows|win32/i'$u_agent)) {
        
$platform 'windows';
    }
  
    
// Next get the name of the useragent yes seperately and for good reason
    
if (preg_match('/MSIE/i'$u_agent) && !preg_match('/Opera/i'$u_agent)) {
        
$bname 'Internet Explorer';
        
$ub    "MSIE";
    } elseif (
preg_match('/Firefox/i'$u_agent)) {
        
$bname 'Mozilla Firefox';
        
$ub    "Firefox";
    } elseif (
preg_match('/Chrome/i'$u_agent)) {
        
$bname 'Google Chrome';
        
$ub    "Chrome";
    } elseif (
preg_match('/Safari/i'$u_agent)) {
        
$bname 'Apple Safari';
        
$ub    "Safari";
    } elseif (
preg_match('/Opera/i'$u_agent)) {
        
$bname 'Opera';
        
$ub    "Opera";
    } elseif (
preg_match('/Netscape/i'$u_agent)) {
        
$bname 'Netscape';
        
$ub    "Netscape";
    }
  
    
// finally get the correct version number
    
$known   = array(
        
'Version',
        
$ub,
        
'other'
    
);
    
$pattern '#(?<browser>' join('|'$known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!
preg_match_all($pattern$u_agent$matches)) {
        
// we have no matching number just continue
    
}
  
    
// see how many we have
    
$i count($matches['browser']);
    if (
$i != 1) {
        
//we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        
if (strripos($u_agent"Version") < strripos($u_agent$ub)) {
            
$version $matches['version'][0];
        } else {
            
$version $matches['version'][1];
        }
    } else {
        
$version $matches['version'][0];
    }
  
    
// check if we have a number
    
if ($version == null || $version == "") {
        
$version "?";
    }
  
    return array(
        
'userAgent' => $u_agent,
        
'name' => $bname,
        
'version' => $version,
        
'platform' => $platform,
        
'pattern' => $pattern
    
);
}
// now try it$ua          getBrowser();$yourbrowser "Your browser: " $ua['name'] . " " 

   $ua['version'] . " on " $ua['platform'] . 
   " reports: <br >" $ua['userAgent'];print_r($yourbrowser);?>

Sample .htaccess file

RewriteEngine On

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php


## forward to www.portal.com if need
RewriteRule ^(.*)$ http://www.portal.com/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]

# if url start with 'index' then execute index.php with params
RewriteRule ^index([a-zA-Z0-9_-\s]+)$ index.php

## if you hit in browser a.html, execute a.php
RewriteRule ^(.*).html$ $1.php

## if you hit cinema execute index.php, or hit pritom execute pritom.php
RewriteRule ^cinema$ index.php
RewriteRule ^pritom$ pritom.php

## if you hit /menu/1/2/3/4, execute menu.php with parameters
RewriteRule ^menu\/(.*)\/(.*)\/(.*)\/(.*)$          menu.php?param1=$1&param2=$2&param3=$3&param4=$4
RewriteRule ^menu\/(.*)\/(.*)\/(.*)$                menu.php?param1=$1&param2=$2&param3=$3
RewriteRule ^menu\/(.*)\/(.*)$                      menu.php?param1=$1&param2=$2
RewriteRule ^menu\/(.*)$                            menu.php?param1=$1

## if not file or directory, execute error.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.+)$ error.php

## before execute any php file, execute before_action.php
php_value auto_prepend_file before_action.php

## after execute any php file, execute after_action.php if immediate file not end with exit
php_value auto_append_file after_action.php

## if not running with https forward to https
RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

## need user authentication
AuthUserFile C:\\xampp\\htdocs\\test\\.htpasswd
AuthName "Authorization Required"
AuthType Basic
require valid-user

Redirect To SSL Using Apache’s .htaccess

There are plenty of times I want to require users to be accessing a site only via SSL. My first try at this was to simple create a .htaccess file that contained SSLRequireSSL, which basically tells Apache that access to a site can only be allowed if SSL is being used. This accomplished what I wanted, but it brought a side issue to requiring SSL, users often leave off (or forget) the the s in https. So after a little bit of digging around I found another approach to this. The new .htaccess file looks like this:


RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
The first line tells Apache we are going to use mod_rewrite. The second line only matches if the port being used to access the site is 443 (the port reserved for https use). If that second line matches then the third takes kicks in, which simply redirects the user to the SSL version of your URL. This still enforces the use of SSL, but saves you from trying figure why you can’t get to your site just because you forget the s in https.

Execute php script before every php script

  1. Make a .htaccess file in document root.
  2. Write the following code:
  3. php_value auto_prepend_file before_action.php
    php_value auto_append_file after_action.php
  4. create a file before_action.php in document root
  5. create a file after_action.php in document root
  6. or you can use full file path such: /var/www/html/site.com/before_action.php

http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file

Password-Protect a Directory With .htaccess


Warning: On at least some versions of Ubuntu, .htaccess files will not work by default. See EnablingUseOfApacheHtaccessFiles for help on enabling them.
Create a file called .htaccess in the directory you want to password-protect with the follwing content:
 
AuthUserFile /your/path/.htpasswd
AuthName "Authorization Required"
AuthType Basic
require valid-user


instead of valid-user, you can also add the users you want directly
If you want to password protect just a single file in a folder add the following lines to the .htaccess file:
 
<Files "mypage.html">
  Require valid-user
</Files>


Then create the file /your/path/.htpasswd which contains the users that are allowed to login and their passwords. We do that with thehtpasswd command:
 
htpasswd -c /path/to/your/.htpasswd user1


The -c flag is used only when you are creating a new file. After the first time, you will omit the -c flag, when you are adding new users to an already-existing password file. Otherwise you will overwrite the file!!
Nevertheless, you should store the file in as secure a location as possible, with whatever minimum permissions on the file so that the web server itself can read the file.
Finally we need to add the following lines to /etc/apache2/apache2.conf:
 
<Directory /your/path>
AllowOverride All
</Directory>


You have to adjust /your/path/.htpasswd
Restart your webserver:
 
sudo /etc/init.d/apache2 restart

Troubleshooting


If you can't access your stuff and the dialog keeps popping up, check that you entered the username and password correctly. If it still doesn't work, check the path to your .htpasswd and make sure the path specified in the AuthUserFile directive is correct. Also make sure that both the.htpasswd and .htaccess files are readable by the web server user chmod 644 should do the trick!

Example


Here is an example on how to prevent users from access the directory, password-protect a specific file and allow userse to view a specific file:
AuthUserFile /your/path/.htpasswd
AuthName "Authorization Required"
AuthType Basic
Order Allow,Deny
<Files myfile1.html>
 Order Allow,Deny
 require valid-user
</Files>

<Files myfile2.html>
 Order Deny,Allow
</Files>

Redirect requests using .htaccess and mod_rewrite


  1. Make sure Apache .htaccess is enabled (by default it is enabled in Ubuntu)
  2. Make sure the Apache module mod_rewrite is enabled. Execute:
sudo a2enmod rewrite

..and see if rewrite is listed here:
sudo apache2ctl -M

and then you can redirect requests using RewriteRules. Example:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_REQUEST=$1 [L]

If you are running windows as your server, create a file .htpasswd at any location with the content.
pritom:pritom
ajay:ajay

The Six Most Common Htaccess Problems and How to Fix Them

Htaccess Problem #1: No Dot Before the Filename

The .htaccess file’s filename must start with a dot, like so:
.htaccess
Files and directory names starting with a dot are treated as hidden files by Unix, Linux & Mac. The htaccess file is hidden so it doesn’t distract from normal web content like HTML files. See hidden files for more information.
Without the dot at the beginning, Apache will ignore the htaccess file.

Htaccess Problem #2: Filename Not All Lowercase

If an htaccess file’s name contains uppercase letters, it generally will not work on Linux or Unix. This is because filenames on Linux and Unix are usually case sensitive. If an htaccess file has any uppercase letters, e.g. .HTACCESS or .HTaccess, Apache won’t find the htaccess file. The htaccess is ignored.
An htaccess file containing uppercase letters generall will work on Windows and Mac. This is because filenames on these platforms are generally case insensitive. I’d recommend sticking with lowercase, so your websites are portable to Linux servers. The use of uppercase letters could also circumvent Apache directives designed to prevent the contents of .htaccess files read from the web.

Htaccess Problem #3: Filename Misspelt

Common misspellings of the htaccess file’s name are htacess and htacess. Check the filename has two c’s and two s’s.

Htaccess Problem #4: Htaccess Disabled by AllowOverride Setting

On some servers, Apache is configured to ignore some or all directives in .htaccess files. This is for security reasons. The AllowOverride directive controls which features will be allowed in .htaccess files. For example AllowOverride None can turn off htaccess files for a folder and its subfolders.
Check your Apache configuration file for which AllowOverride directive is applied to the directory containing your problem htaccess file.
If you’re not sure which configuration file to look in, start with the main Apache configuration file httpd.conf or apache2.conf. If your website is configured in a file included by httpd.conf (e.g. a virtual hosts configuration file), you will need to look in that file. See Location of httpd.conf on CentOS, Ubuntu, Mac and others to locate your httpd.conf.
To enable using a .htaccess file, change AllowOverride None to AllowOverride All.
For example, for a CentOS 5.3 server, I needed to change the AllowOverride setting in the file /etc/httpd/conf.d/virtualhosts.conf.
httpd.conf before:
Options FollowSymLinks
AllowOverride None
httpd.conf after:
Options FollowSymLinks
AllowOverride All
Be aware that enabling htaccess files has security implications, as htaccess files override your Apache configuration. For example, if your site provides uploads, a hacker could potentially upload a .htaccess file to your server and use it to gain access to your server. There are options to AllowOverride that restrict the directives that will be used from a .htaccess file. See the documentation for AllowOverride.

Htaccess Problem #5: Incorrect Syntax

If Apache can’t understand a line in your htaccess, it will usually cause an error from Apache. The error may show in the web browser when a webpage causes Apache to read the .htaccess file.
To demonstrate this, I added a bad line to the Smart Web Developer .htaccess file. See if you can spot the bad line.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
utter rubbish config line here
This causes the following error in the browser:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
The reason for the error is logged to an Apache error log:
[Sat Feb 26 18:21:09 2011] [alert] [client 127.0.0.1] /Users/taz/Sites/smartwebdeveloper/.htaccess: Invalid command 'utter', perhaps misspelled or defined by a module not included in the server configuration, referer: http://smartwebdeveloper.com/
Another option to debug Apache syntax is to put the the .htaccess contents into the main Apache configuration file under a <Directory> directive. Apache has an option to parse and check its configuration files. To run an Apache syntax check, run: httpd -S.

Htaccess Problem #6: Htaccess Settings Overridden by Another Htaccess File

Multiple .htaccess files may be read and applied if a web request is made to a file in nested directories. Typically all .htaccess files in the path between the website’s root directory and the requested file’s directory will be read and applied in order. As a result, an htaccess file in a more deeply nested directory can override the settings made by an htaccess in a higher directory.
Is there another .htaccess file in another directory in the path to your webpage? That htaccess file the may be overriding the settings in the htaccess you’re looking at.
On Mac, Linux & Unix, you can find all .htaccess files on your website in the terminal:
find /path/to/website/root -iname .htaccess -print

http://smartwebdeveloper.com/apache/htaccess-problems 

Monday, July 22, 2013

Get form values from extjs form as urlencode

var values = Ext.getCmp('FORM_NAME').getForm().getValues(true);
OR
var values = Ext.getCmp('FORM_NAME').getForm().getValues(false);