Initialize custom session

Views: 3 Last modified: August 03rd, 2011 Comments: 0

This function might become useful when you desire a function which can store session files in a custom directory, even when the directory has not been created yet, this function will create the directory for you. Besides that, you can provide a session name.

The session name can ONLY CONTAIN alphanumeric characters. If the session name is omitted as argument, it will take the default session name as configured in the php.ini file (ONLY alphanumeric characters). If the session name contains not only alphanumeric characters, it will return false.

<?php
function SaveCustomSession( $dir, $ses_name = '' )
{
	// Check if session name is empty
	if ( empty( $ses_name ) )
		$ses_name = ini_get( 'session.name' );

	// If session name isnt alphanumeric, return false
	if ( ( bool )preg_match( '@^[a-z0-9]+$@i', $ses_name ) === false )
		return false;

	// Check if the directory already exists
	if ( ! is_dir( $dir ) )
	{
		// Set created to false
		$created = false;

		// If dir created
		if ( @mkdir( $dir ) )
			$created = true;

		// If created = false
		if ( ! $created )
			return false;
	}

	// Custom session name
	session_name( $ses_name );

	// If the directory exists or has been created, use as save path
	session_save_path( $dir );

	// Initialize session
	session_start();
}
?>

Example os use

<?php
// Example of use
SaveCustomSession('C:\server\sessionsss', 'Navigation');
?>
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
    Bluehost

    Mail this!

    To: From:Sum {6+7} =  
    Anything to add ?

        You must be logged in to post a comment.