Custom session save path

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

This function bundles the ability to create a custom directory and use that for the save path of session files. But if the directory already exists, it will be used for saving the session files without any modifications. This function needs to be called before session_start()

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

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

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

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

}
?>

Create quickly a custom directory for session files example

// Specify session files save path
SaveCustomSession('C:\server\sessions');

// Initialize session
session_start();
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {0+3} =  
    Anything to add ?

        You must be logged in to post a comment.