[Solved] What effect does this code in PHP? [closed]


As you pasted it in one liner

<?php
session_name('MoodleSession'.$CFG->sessioncookie); //设置当前session名称为MoodleSession /* * */ if (check_php_version('5.2.0')) { session_set_cookie_params(0, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly); } else { session_set_cookie_params(0, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure); }

the only executed function would be session_name('MoodleSession'.$CFG->sessioncookie); because you are commenting the rest of the line with a //

What does this piece of code?

<?php
@session_start();

If you prepend a piece of code in PHP with an @, the PHP interpreter will not throw any warnings, which is a bad practice and error prone.

session_start() as defined by http://php.net/:

session_start() creates a session or resumes the current one based on
a session identifier passed via a GET or POST request, or passed via a
cookie.

When session_start() is called or when a session auto starts, PHP will
call the open and read session save handlers. These will either be a
built-in save handler provided by default or by PHP extensions (such
as SQLite or Memcached); or can be custom handler as defined by
session_set_save_handler(). The read callback will retrieve any
existing session data (stored in a special serialized format) and will
be unserialized and used to automatically populate the $_SESSION
superglobal when the read callback returns the saved session data back
to PHP session handling.

If you need more info I can update my answer. Maybe you should paste your code better, I think you have it bad formatted.

UPDATE:

Okay, saw your new code 😛

As my previous statement about @session_start() stands, your code just updates a session name to "MoodleSession" plus the value of $CFG->sessioncookie.

The middle part

if (check_php_version('5.2.0')) {
    session_set_cookie_params(0, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure, $CFG->cookiehttponly);
} else {
    session_set_cookie_params(0, $CFG->sessioncookiepath, $CFG->sessioncookiedomain, $CFG->cookiesecure);
}

checks the PHP version. According to the changelog of session_set_cookie_params(), a httponly parameter was added, so in case of running the PHP 5.2 version, it will execute session_set_cookie_params with the extra parameter.

solved What effect does this code in PHP? [closed]