1

I want to generate a session ID. This unique sessionId represents some different data for every different user.

My code till now:

  1. onclick and onload of a page I call a function create_session_id();

    function create_session_id() {
        // This function sends a string to a PHP page using ajax post.
    }
    
  2. On my PHP page, I receive this data and then insert it:

    session_start();
    $a = session_id();
    $object->insert_sessionid($a);
    

My Question

Is it possible to use only session_start() (no session_id()) and store some unique session ID value that I can use later when I fetch data and throw data to my web page. I don’t want any clicks to register sessionid. It should happen on page load and without any function create_session_id().

I am thinking to bring some cookies in to the picture.

Note: My website doesn’t allow login.

4

2 回答 2

4

使用类似的功能

function createRandomVal($val){
      $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,-";
      srand((double)microtime()*1000000);
      $i = 0;
      $pass = '' ;
      while ($i<=$val) 
    {
        $num  = rand() % 33;
        $tmp  = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
      }
    return $pass;
    }

并在会话 ID 中传递返回值

于 2013-07-11T14:55:10.463 回答
1

上面的代码在编辑后有点错误user669677

这是正确的代码:

function createRandomVal($val) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,-";
    srand((double)microtime() * 1000000);
    $i = 0;
    $pass = '';
    while ($i < $val) {
        $num = rand() % 64;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
于 2017-11-23T01:12:16.147 回答