title: session_start() issues regarding illegal characters, empty session ID and failed session
body: <p><strong>So, I realise this is a repeat question</strong> however, it is apprently a bug yet the original post for this is 5 years old but it's also said that it's a malicious attack... <a href="https://stackoverflow.com/questions/3185779/the-session-id-is-too-long-or-contains-illegal-characters-valid-characters-are">The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and &#39;-,&#39;</a></p>
  <p>What is the latest, <strong>correct way</strong> of dealing with this issue?</p>
  <p>My error logs show:</p>
  <blockquote>
  <p>[30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/ACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27</p>
  <p>[30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/ACCOUNT/public_html/wp-content/plugins/cusplugin/cusplugin.php on line 21</p>
  <p>[30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/ACCOUNT/public_html/wp-content/plugins/cusplugin/cusplugin.php on line 377</p>
  <p>[30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/ACCOUNT/public_html/wp-content/plugins/cusplugin/cusplugin.php on line 718</p>
  <p>[30-Sep-2015 10:12:50 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0</p>
  <p>[30-Sep-2015 10:12:50 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0</p>
  </blockquote>
  <h2>My full logs:</h2>
  <pre><code>[30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [30-Sep-2015 10:12:37 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [30-Sep-2015 11:12:37 Europe/London] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 718
  [30-Sep-2015 11:12:37 Europe/London] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [30-Sep-2015 10:12:49 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [30-Sep-2015 10:12:50 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [30-Sep-2015 10:12:51 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 718
  [30-Sep-2015 10:12:51 UTC] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [30-Sep-2015 10:12:53 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [30-Sep-2015 10:12:53 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [30-Sep-2015 10:12:53 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [30-Sep-2015 10:12:53 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [30-Sep-2015 10:12:53 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [30-Sep-2015 10:13:04 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [30-Sep-2015 10:13:04 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [30-Sep-2015 10:13:04 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [30-Sep-2015 10:13:04 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 718
  [30-Sep-2015 10:13:04 UTC] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [01-Oct-2015 04:47:21 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [01-Oct-2015 04:47:21 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [01-Oct-2015 04:47:21 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [01-Oct-2015 04:47:21 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [01-Oct-2015 04:47:21 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [01-Oct-2015 05:47:22 Europe/London] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [01-Oct-2015 05:47:22 Europe/London] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [01-Oct-2015 04:47:22 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [01-Oct-2015 04:47:22 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 718
  [01-Oct-2015 04:47:22 UTC] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [01-Oct-2015 04:47:24 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [01-Oct-2015 04:47:24 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 377
  [01-Oct-2015 04:47:24 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [01-Oct-2015 04:47:24 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [01-Oct-2015 04:47:24 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [01-Oct-2015 23:10:23 UTC] PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 492
  [01-Oct-2015 23:11:15 UTC] PHP Warning:  in_array() expects parameter 2 to be array, null given in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 492[02-Oct-2015 08:59:42 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [02-Oct-2015 08:59:42 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [02-Oct-2015 08:59:42 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [02-Oct-2015 09:59:42 Europe/London] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 739
  [02-Oct-2015 09:59:42 Europe/London] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [02-Oct-2015 08:59:44 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [02-Oct-2015 08:59:45 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [02-Oct-2015 08:59:45 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [02-Oct-2015 08:59:45 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [02-Oct-2015 08:59:45 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [02-Oct-2015 08:59:46 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [02-Oct-2015 08:59:46 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [02-Oct-2015 08:59:46 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [02-Oct-2015 08:59:46 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 739
  [02-Oct-2015 08:59:46 UTC] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [02-Oct-2015 08:59:52 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [02-Oct-2015 08:59:52 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [02-Oct-2015 08:59:52 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [02-Oct-2015 08:59:52 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [02-Oct-2015 08:59:52 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [03-Oct-2015 04:51:46 UTC] PHP Warning:  require(ABSPATHwp-includes/load.php): failed to open stream: No such file or directory in /home/HOSTINGACCOUNT/public_html/wp-settings.php on line 21
  [03-Oct-2015 04:51:46 UTC] PHP Warning:  require(ABSPATHwp-includes/load.php): failed to open stream: No such file or directory in /home/HOSTINGACCOUNT/public_html/wp-settings.php on line 21
  [03-Oct-2015 04:51:46 UTC] PHP Fatal error:  require(): Failed opening required 'ABSPATHwp-includes/load.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/HOSTINGACCOUNT/public_html/wp-settings.php on line 21
  [03-Oct-2015 08:09:48 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [03-Oct-2015 08:09:48 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [03-Oct-2015 08:09:48 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [03-Oct-2015 09:09:49 Europe/London] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 727
  [03-Oct-2015 09:09:49 Europe/London] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [03-Oct-2015 08:09:52 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [03-Oct-2015 08:09:52 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [03-Oct-2015 08:09:52 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [03-Oct-2015 08:09:52 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [03-Oct-2015 08:09:52 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  [03-Oct-2015 08:09:55 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [03-Oct-2015 08:09:55 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [03-Oct-2015 08:09:55 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [03-Oct-2015 08:09:55 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 727
  [03-Oct-2015 08:09:55 UTC] PHP Fatal error:  Call to a member function check_connection() on a non-object in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/simple-press/sp-api/sp-api-wpdb.php on line 439
  [03-Oct-2015 08:09:57 UTC] PHP Warning:  session_start(): The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/wl-coupon/wishlist-coupon20.php on line 27
  [03-Oct-2015 08:09:57 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 385
  [03-Oct-2015 08:09:57 UTC] PHP Warning:  session_start(): Cannot start session with empty session ID in /home/HOSTINGACCOUNT/public_html/wp-content/plugins/customplugin/customplugin.php on line 21
  [03-Oct-2015 08:09:57 UTC] PHP Warning:  Unknown: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in Unknown on line 0
  [03-Oct-2015 08:09:57 UTC] PHP Warning:  Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
  </code></pre>
  <p>As you can see there they all seem to happen in groups just look at 30th of september it happens loads of times all within 1 min then doesn't happen the rest of the day...</p>
  <p>It's throwing the sessionid issue up for customplugin, simple press forums (I think),  and Wishlist Coupon 2.0</p>
  <p>My customplugin code has:</p>
  <pre><code>if(!session_id()) {
      session_start();
  }
  </code></pre>
  <p><strong>I've tried option 2 from below but it didn't help/fix the problem.</strong></p>
  <p>A snippet of the other WordPress plugin that is causing the some of the error(s):</p>
  <pre><code>class WishListCoupon20 extends WishListPlugin {
      public function __construct($file, $slug, $sku, $name, $link_name, $prefix, $require_wlm) {
          parent::__construct($file, $slug, $sku, $name, $link_name, $prefix, $require_wlm);
  
          session_start();
  </code></pre>
  <p>The other stack overflow post has a few variations of working around the issue but I am not sure what is correct because the post is 5+ years old and you would expect a bug to have been fixed in that time.</p>
  <h2>Option 1 thanks to Sergey Eremin:</h2>
  <pre><code>&lt;?php
      function my_session_start()
      {
          if (ini_get('session.use_cookies') &amp;&amp; isset($_COOKIE['PHPSESSID'])) {
              $sessid = $_COOKIE['PHPSESSID'];
          } elseif (!ini_get('session.use_only_cookies') &amp;&amp; isset($_GET['PHPSESSID'])) {
              $sessid = $_GET['PHPSESSID'];
          } else {
              session_start();
              return false;
          }
  
          if (!preg_match('/^[a-z0-9]{32}$/', $sessid)) {
              return false;
          }
          session_start();
  
          return true;
      }
  ?&gt;
  </code></pre>
  <h2>Option 2 thanks to danjfoley:</h2>
  <pre><code>try {
     session_start();
  } catch(ErrorExpression $e) {
     session_regenerate_id();
     session_start();
  }
  </code></pre>
  <h2>Option 3 thanks to Cendak (using Andron's previous solution)</h2>
  <pre><code>function my_session_start()
  {
        $sn = session_name();
        if (isset($_COOKIE[$sn])) {
            $sessid = $_COOKIE[$sn];
        } else if (isset($_GET[$sn])) {
            $sessid = $_GET[$sn];
        } else {
            return session_start();
        }
  
       if (!preg_match('/^[a-zA-Z0-9,\-]{22,40}$/', $sessid)) {
            return false;
        }
        return session_start();
  }
  
  if ( !my_session_start() ) {
      session_id( uniqid() );
      session_start();
      session_regenerate_id();
  }
  </code></pre>
  <h2>Option 4 thanks to Andron:</h2>
  <pre><code>&lt;?php
      function my_session_start()
      {
          $sn = session_name();
          if (isset($_COOKIE[$sn])) {
              $sessid = $_COOKIE[$sn];
          } else if (isset($_GET[$sn])) {
              $sessid = $_GET[$sn];
          } else {
              session_start();
              return false;
          }
  
         if (!preg_match('/^[a-zA-Z0-9,\-]{22,40}$/', $sessid)) {
              return false;
          }
          session_start();
  
         return true;
      }
  ?&gt;
  </code></pre>
  <h2>Option 5 thanks to alpere:</h2>
  <pre><code>$ok = @session_start();
  if(!$ok){
  session_regenerate_id(true); // replace the Session ID
  session_start(); 
  }
  </code></pre>
  <h1>Or... is there a better way.</h1>