PHP Style Cookie and Get Parameters in JavaScript

Submitted by pure-php on Thu, 2006-02-23 08:56.

For lazy phpers, with this function you can read cookies and get parameters in javascript in php style.
(I use php tags to highlight the code)
Example

<?php
populateRequest
();
if( 
$_COOKIE['sid'] ) {
// do something
}
if( 
$_GET['cmd'] == 'login' ) {
// do something
}
?>

<?php
function populateRequest(){
    
String.prototype.trim = function(){
        return 
this.replace(/^s+|s+$/, '');
    }
    
cookies document.cookie.split(';');
    
window.$_COOKIE = new Object();
    for(var 
0cookies.lengthi++){
        
curentCookie cookies[i];
        var 
ckv curentCookie.split('=');
        var 
ckv[0];
        var 
ckv[1];
        if( 
&& ){
            
k.trim();
            
$_COOKIE[k] = v;
        }
    }
    var 
query location.search.replace('?''');
    
params query.split('&');
    
window.$_GET = new Object();
    for(var 
0params.lengthi++){
        
curentParam params[i];
        var 
ckv curentParam.split('=');
        var 
ckv[0];
        var 
ckv[1];
        if( 
&& ){
            
k.trim();
            
$_GET[k] = v;
        }
    }
}
?>

add new comment

Datenschutz | Impressum