0

When update my PHP version to 5.4 (xampp 1.8.0) I see a Fatal error:

Fatal error: Cannot redeclare class Session in C:\xampp\htdocs\vs\classes\session.class.php on line 5

Is this problem related to update PHP version's?

session.class.php:

<?php
defined('_VALID') or die('Restricted Access!');

class Session
{   // <-- This Is Line 5 
    private static $_sess_db;

    public static function open() {
        global $config;    

        if (self::$_sess_db = mysql_connect($config['db_host'], $config['db_user'], $config['db_pass'])) {
            return mysql_select_db($config['db_name'], self::$_sess_db);
        }

        return false;
    }

    public static function close() {
        return mysql_close(self::$_sess_db);
    }

    public static function read($session_id) {
        $sql = sprintf("SELECT `session_data` FROM `sessions` WHERE `session_id` = '%s'", mysql_real_escape_string($session_id));
        if ($result = mysql_query($sql, self::$_sess_db)) {
            if (mysql_num_rows($result)) {
                $record = mysql_fetch_assoc($result);
                return $record['session_data'];
            }
        }

        return '';
    }

    public static function write($session_id, $session_data)
    {
        $sql = sprintf("REPLACE INTO `sessions` VALUES('%s', '%s', '%s')", mysql_real_escape_string($session_id),
                        mysql_real_escape_string(time()), mysql_real_escape_string($session_data) );

        return mysql_query($sql, self::$_sess_db);
    }

    public static function destroy( $session_id )
    {
        $sql = sprintf("DELETE FROM `sessions` WHERE `session` = '%s'", $session_id);
        return mysql_query($sql, self::$_sess_db);
    }

    public static function gc($max) {
        $sql = sprintf("DELETE FROM `sessions` WHERE `session_expires` < '%s'", mysql_real_escape_string(time() - $max));
        return mysql_query($sql, self::$_sess_db);
    }
}
2
  • 1
    how is the page being called? It sounds like you have a double include or require somewhere. try using include_once or require_once instead Commented Aug 2, 2012 at 19:25
  • Asked many, many times before Commented Aug 2, 2012 at 19:32

2 Answers 2

4

It seems that you are including session.class.php more than once... or you declare the class Session in another file that is included.

As @cegfault says: include_once and require_once can be used to prevent this.

But it is better to make sure that you only include the file just once (less memory usage). include_once and require_once are just fail saves.

Sign up to request clarification or add additional context in comments.

2 Comments

be sure to mention that include_once and require_once can be used to prevent this
i including session.class.php more than once, so now fixed with removing One of them. Thanks.
1

Try wrapping your class in if statement like this:

if (class_exists('Session') != true) {

    class Session { ... }

}

This will prevent the error, if your code structure is complex/large

See the class_exists() documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.