Skip to content

Drupal Sessions

2009 June 25
Posted by bharatsharma83@gmail.com

Drupal sessions:

How Sessions works on drupal:

The page request send by the browser includes a 32-character ID known as PHPSESSID under set- cookie tag, which is used as the key to the session information that drupal stores about session.

  • The settings for session management are done in three files i.e. .htaccess, settings.php nad bootstarp.inc

  • By default drupal uses the cookie based session management instead of URL base session management. If the browser doesn’t accept cookies , a session cannot be established

      • The PHP directive ‘sessions.use_only_cookies’ is set to 1 in settings.php to accept cookie based sessions.

      • The PHP directive ‘sessions.use_trans_sid’ is set to 0 in settings.php to reject URL based sessions.

  • At the installation time Drupal turns off the PHP’s ‘session.auto_start’ functionality in .htaccess file to take full control over sessions.

Why Drupal need sessions?

  • To handle the viewing preferences of the site content according to viewer status.

  • To remember the viewing preferences: Drupal stores sessions in ‘sessions’ table, the session table is used to associate the sessions Ids with the user ids to retrieve the viewing preferences of a user. It applies a join query on session and user tables using session ids and user ids to know the user role. Session data of a user is viewable by $user->session of $user object.

      • To represent a session for anonymous user, the ‘session’ table ‘uid’ column is set to 0.

      • To represent a session for login user, the ‘session’ table ‘uid’ column is set to user Id with a new session Id such that the session does’nt get hijacked. If a user logs out, the row for that session is removed from the database immediately.

  • To remember the comments preferences: In comment.module session is used to store viewing preferences for users, i.e.

      • $_SESSIONS['comment_mode']=$mode

      • $_SESSIONS['comment_sort']=$order

      • $_SESSIONS['comment_comments_per_page']=$comments_per_page

  • To handle file uploads

Session Life time

  • Sessions lifetime related settings are located in settings.php, By default

      • ini_set(‘session.cache_expire’, 200000); // 138.9 days

      • ini_set(‘session.cookie_lifetime’, 2000000); // 23.1 days

      • ini_set(‘session.gc_maxlifetime’, 200000); // 55 hours

Other Information

  • Writing session in the session table is the last task of drupal while serving pages.

  • Drupal does not store session information , the first time a user visits a site. This is to reduce the load on the sessions table generated by crawlers.

  • Drupal $user object is first built during the DRUPAL_BOOTSTRAP_SESSION phase of bootstrapping by sess_read().

  • While deploying drupal on multiple subdomains, you can create a unique session name for each site by adding ini_set(‘session.name’, ‘my-site-unique-name_PHPSESSID’); in settings.php

  • If you need to store short term data for anonymous users, use the $_SESSION superglobal e.g.

      • $_SESSION['anonymous_data']=$anonymous_data;

  • If you want to store a permanent data to a user, save it in the $user object e.g.

      • $user->login_name=$login_name; user_save();

csmpqgbe7r

Add to Technorati Favorites

Attach with Intel’s Ideateup

2010 July 16
Comments Off
Posted by bharatsharma83@gmail.com

This is great link must to read:

http://ideateup.ideascale.com/

Joomla modules layout override

2010 May 31
Comments Off
Posted by bharatsharma83@gmail.com

Every one working as Drupal Developer must know how to customize the layout of any module output i.e. theming functions and hook_theme…The similar functionality of customizing modules output is also exist in Joomla. i.e. Joomla is similarly powerfull enough to utilise the MVC  architecture to control over layout of modules and components easily.

To Do the customization you only require to add the default.php file ( mostly found under tmpl folder of a module) in your current template i.e. like this

1 Create a folder called html in the current template.

2. Create another folder in above mentioned folder called mod_module_name_to _override.

3. add the default.php file of the module you want to customize in above created folder and then you can change the code of default.php according to your desire.

Hence the path to your new customized file should be similar to this:

templates/your_template_folder/html/mod_module_name_to_override/layout_file_name.php

JPane Class in Joomla

2010 May 31
Posted by bharatsharma83@gmail.com

Recently working on a Property project in Joomla I found myself stucked when i got a requirement to show videos, images, location and panoramic street view in tabs. I know there are lots of modules available in joomla to create tabs but my client do not wish an extra module load Hence the only way to implement the functionality in property component and bingo I got Jpane Class at right time.

Yes Most of us used to with the available multitabs modules in joomla but if it comes to Joomla classes to code a similar effect , We have Jpane Class ready to do the magic.

Here it goes:

jimport('joomla.html.pane');
//1st Parameter: Specify 'tabs' as appearance
//2nd Parameter: Starting with third tab as the default (zero based index)
//open one!
$pane =& JPane::getInstance('tabs', array('startOffset'=>2));
echo $pane->startPane( 'pane' );
echo $pane->startPanel( 'Example Panel 1', 'panel1' );
echo "This is panel1";
echo $pane->endPanel();
echo $pane->startPanel( 'Example Panel 2', 'panel2' );
echo "This is panel2";
echo $pane->endPanel();
echo $pane->startPanel( 'Example Panel 3', 'panel3' );
echo "This is panel3";
echo $pane->endPanel();
echo $pane->endPane();

Options 

startOffset: The default tab to start with.
onActive: Another function to use when making a tab active (??)
onBackground: Another function to use when making a tab dissapear (??)

Jpane can be used for both tabs or sliders.

I implemented the Jpane code in the propery component like this: Showing in rough manner.

<?php
jimport(‘joomla.html.pane’);
$pane =& JPane::getInstance(‘tabs’, array(‘startOffset’=>0));
echo $pane->startPane( ‘pane’ );

/*….start Video tab *****/
if($ActivarDescripcion == 1){
echo $pane->startPanel( ‘<p>Video</p>’, ‘panel1′ );
}

echo ‘<object width=”478″ height=”373″><param name=”movie” value=”http://www.youtube.com/v/iKKqeeCqTM4&hl=en_US&fs=1&”></param><param name=”allowFullScreen” value=”true”></param><param name=”allowscriptaccess” value=”always”></param><embed src=”http://www.youtube.com/v/iKKqeeCqTM4&hl=en_US&fs=1&” type=”application/x-shockwave-flash” allowscriptaccess=”always” allowfullscreen=”true” width=”478″ height=”373″></embed></object>’;
echo $pane->endPanel();
/*….End  Video tab *****/

/*….start Location/ Google Map tab *****/
if($ActivarDetails == 1){
echo $pane->startPanel( ‘<p>Location</p>’, ‘panel1′ );
}
if($ActivarMapa == 1){
$apikey=$params->get(‘apikey’);
$distancia=$params->get(‘distancia’);
$lat = $Product->lat;
$lng = $Product->lng;
?>

<script src=”http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo $apikey;?>”
type=”text/javascript”></script>

<script type=”text/javascript”>
function loadmap() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById(“map”));
map.setCenter(new GLatLng(<?php echo $lat;?>, <?php echo $lng;?>), <?php echo $distancia;?>);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
var marker = new GMarker(new GLatLng(<?php echo $lat;?>, <?php echo $lng;?>));
map.addOverlay(marker);
}
}
</script>

<div id=”map” style=”width: 520px; height: 420px;margin:0px;padding:0px;”>
</div>

<script type=”text/javascript”>  loadmap();  </script>
<div style=”clear: both;”></div>

<?php

}

echo $pane->endPanel();
/*….End Location/ Google Map tab *****/
?>

For any confusion on JPane you can contact me at bharat@rhaasoft.com

www.rhaasoft.com

Serving IT Serving You

Mod_placehere : Some point to consider

2010 May 31
Comments Off
Posted by bharatsharma83@gmail.com

Mod_placehere is quite a nice module , easy to use and deploy the articles on any page in form of a module. However while using Mod_placehere module in Joomla I came to know some point to consider i.e.

mod_placehere is not using meta data of articles to show on each page that means the global meta data(keywords and description) would be viewable on every page where articles are rendered through mod_placehere module.

So I did some hack in module code to show the keywords and description of each article instead of global keywords and description.

Those who are using the mod_placehere module can use this hack for metadata rendering.

Here is the code I used:

Go to modules/mod_placehere/tmpl

You should find some files like

beez.php

beezArticleParams.php

default.php

menustyle.php

titlesonly.php

Open all these files and find the function “ob_start()“.

Below that function add following lines:

$document =& JFactory::getDocument();
$document->setMetaData(“keywords”,$article->metakey);
$document->setDescription($article->metadesc);

that’s it , Now your site will show related metadata for each article rendered via mod_placehere.

Another Way: There is another way to implement the above lines via single file i.e. using “include_once” function.


Noderorder with views

2010 May 31
Comments Off
Posted by bharatsharma83@gmail.com

Have you ever wanted to have an ordering system in drupal similar to joomla i.e. you can show the list of nodes acc. to the order you want to set instead of just setting it up in ascending or descending or by view sort criteria.

I just encountered a similar issue in my one of the proejct ..The client wish to have a simple way to set nodes order by just drag and drop functionality. and voilla I found Noderorder module.

There are other similar modules in drupal that might work well ( i havnt tried that) However Nodeorder gives the similar functionality which my client wished.

Ok I Installed Nodeorder on my drupal site and followed the steps according to the readme file. But I havent found it working correctly..No Problem I found the reason..i.e. i am using a view to show the list of nodes hence I applied following steps to implement nodeorder with views:

  1. Installed NodeOrder
  2. Created a taxonomy vocabulary called “order nodes by category” and checked the multiple select box to show this vocabulary in content type “News” and check “ordering” check box too .
  3. Added a term “order news” under newly created vocabulary “order nodes by category”.
  4. Now create a view which would filter nodes on the basis of taxonomy term “order news” and define a sort criteria on basis of taxonomyterm id weight.
  5. Add nodes in News Content type and select “order news”  for each node of news.
  6. Now go to the taxonomy term “order news” in taxonomy and you can see there listing of the nodes you created in news content type. Thats it, now you can order the nodes there.

Hope I gave an easy step by step explanation. Feel free to contact me for any queries at bharat@rhaasoft.com

www.rhaasoft.com

Override Search box default value in Drupal

2010 May 31
Comments Off
Posted by bharatsharma83@gmail.com

Ever wonder how do you show some text in the Drupal default search box without using jquery_plugin or jquery labelify scripts.

Yes you can as i did on one of my client website here http://www.propelictqa.com/ . I created a simple module to overite the default value of search input field via hook_form_alter and added an attribute for onfocus and onblur event.

Like this:

function yourmodulename_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case ‘search_block_form’:
$form[$form_id] = array(
‘#title’ => t(”),
‘#type’ => ‘textfield’,
‘#size’ => 15,
‘#default_value’ => t(‘Your default Value’),

‘#attribute’ =>array(‘onfocus’=>’if(this.value=\’Your default Value\’){this.value=\’\'};’,

‘onblur’=>’if(this.value=\’\'){this.value=\’Your default Value\’};’,

);

break;
}
}

The above is the simple form_alter code which you can apply to any of your custom module to work with although the functionality of this module can be widened by adding some permissions, hook_menu and an admin settings form where you can give your client an access to change  the value of search box anytime without disturbing the code.

I did the same i.e. created a module with admin settings to change the text anytime from admin panel of my site.

However that requires a good time to write all here. but if any body wants that functionality i am happy to send him/her the code.

Please feel free to contact me here bharat@rhaasoft.com

www.rhaasoft.com

Drupal Cache

2009 July 2
Tags: , ,
Posted by bharatsharma83@gmail.com
  • Caching is required to prevent dynamic websites numerous trips to the database to retrieve information about saved content, user, setting and config. Caching is used to speed up a website performance and visibility.
  • Drupal uses its cache API to provide caching of modules, node and pages. The default table in which drupal stores the module settings is called ‘cache’. We can create our own cache tables for various modules. The only requisite of cache API for custom cache tables is that the structure must be identical to the default cache table.
  • Besides cache table drupal also ships with three other cache tables know as

cache_page (stores cache of anonymous pages)

cache_menu (stores cache for navigational menus)

cache_filter ( stores cache copy of node’s content)

  • The drupal_page_cache_header() prepares the cache data by setting HTTP headers.
  • Functions of cache API

Three important cache functions:

cache_set($cid, $table=’cache’, $data, $expire=CACHE_PERMANENT, $header=NULL)

where , $cid= Unique cache ID

$table= Name of the cache table to store the data, by default ‘cache’ table is used.

$data= data to store in the cache.

$expire=The cache data expire time. You can use CACHE_PERMANENT, CACHE_TEMPORARY or a Unix Timestamp. By default CACHE_PERMANENT is set.

$headers= The HTTP header passed to the browser.

cache_get($cid, $table=‘cache’ )

where $cid= The cahe id of the data to retrieve.

$table= The table name from which data retrieved. The cache table is used by Default.

cache_clear_all($cid=NULL,$table=NULL,$wildcard=NULL)

where $cid=if set,  clear only the given cache ID, otherwise delete all expire entries.

$table=the table to delete from.if not set entries from cache_block and cache_page table will be deleted.

$wildcard=if set to TRUE , all cache entries with ID contains a substring similar to $cid wil be deleted.

8yjebzxq9v

Drupal Forms API

2009 June 29
Posted by bharatsharma83@gmail.com

This post is about the Form API of drupal6, You may find this post written in a little bit of unimpressive way, this is bcoz i am running short of time these days nonetheless i would like your suggestions and comments at any time and will try to make them happening:

Forms API

  • The forms API abstracts forms into a nested array of properties and values.
  • $form_values is array which holds form elements values.
  • The form token (form unique id) is stored in the variables table as drupal_private_key.
  • drupal_get_form() function interact with drupal form.
  • hook_elements() function is used to define element type.
  • hook_element() can be used to define your own element types in your module.
  • element_info() hook collects all the properties for all form elements.

FORM PROPERTIES

  • #validate property is used to validate the form elements.
  • #base is used to apply same validator to several forms.
  • #submit handles form submission. If there is no property named #submit, drupal looks for function name “formID_submit()” or failing that drupal again search for “#basevalue_submit”.
  • #after_build property will call all functions of a form.
  • #theme is used to set a theme to an existing form.
  • #pre_render property used to call the functions just before the transformation of form from a data structure to HTML.
  • #prepend, #suffix properties are used to prepend and append text to  the form elements. drupal also uses these properties while rendering the form.
  • #method property is used to set the form method to post. By default it is set to post, Also get method is not supported by the form API.
  • #tree : Setting #tree to TRUE gives a nested array of fields with there values. While #tree is set to FALSE, we get a flattened representation of field name and values.

FLOW of form formation:

when a module contains a form code is called by drupal the form datastructure renders form into html in the following sequence:

call drupal_get_form() -> call hook_elements()['defining element types such as radio buttons, chekcboxes, textfiels'] -> call elements_info() [information about the related properties ,values and functions] -> #validate property -> #submit property

Bibliography: Apress Pro Drupal Devlopement (ycu73k6ath)