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');
?>
Mail this!
- Comments (0)
- PingBacks (0)
- TrackBacks (0)


» Latest comments