-1

Please consider the following code snippet:

From php-5.3.1/ext/session/session.c:

PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS)
…
gettimeofday(&tv, NULL);
…
/* maximum 15+19+19+10 bytes */
spprintf(&buf, 0, "%.15s%ld%ld%0.8F", remote_addr ? 
remote_addr : "", tv.tv_sec, (long int)tv.tv_usec, 
php_combined_lcg(TSRMLS_C) * 10);
…
return buf;
}

I have found it on the internet. But I can't understand what code is this. I guess this is the implementation of a php function in C++. If yes, then please explain me how php calles c++ function in it?

hakre
  • 193,403
  • 52
  • 435
  • 836
Narek
  • 38,779
  • 79
  • 233
  • 389

3 Answers3

4

The shocking truth is that PHP is written in C. You are looking at the source of PHP itself, or need to explain the question further.

Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
  • What does it mean that php is written in C? Does each function of php implemented with the same name in C, and when you call a php function the C function is being called itself? Can I say that PHP is a lib written in C and when I write PHP, actually I write C by using PHP library? – Narek Feb 09 '11 at 13:14
  • @Narek The PHP interpreter is a program, which is written in C. You might want to google "interpreter" to understand what that is. – Jim Balter Feb 09 '11 at 13:24
  • @Narek: See http://stackoverflow.com/questions/2720488/how-exactly-is-a-php-script-executed – Mchl Feb 09 '11 at 13:26
  • No. When you write PHP, your code is being interpreted by the PHP interpreter. This interpreter happens to be written in a different language - C - but which language actually doesn't matter for you. An PHP interpreter could even be written in Java or Assembler. That a lot of PHP function names resemble C standard library function names is a coincidence. – Gunther Piez Feb 09 '11 at 13:30
  • 1
    There is in fact a PHP implementation written in Java: http://www.caucho.com/resin-3.0/quercus/ – Mchl Feb 09 '11 at 13:37
4

It is not a C++ code, it is pure C. The PHP library can call C functions just like any other library implemented in C. The code snippet generates a "unique" session ID consisting of the client address, the current time, and a pseudo-random number from some linear congruential generator.

Philipp
  • 48,066
  • 12
  • 84
  • 109
1

I am guessing that you ended up to that piece of code via the DEFCON 18: How I Met Your Girlfriend lecture? Great talk btw. :-)

Now about the code snippet, it is C and it is part of PHP's code. This exact function handles the generation of PHP session ids. You have the entire function logic explained in the lecture i mentioned above, in case you didn't see it.

As a side not, PHP does not call C functions, instead you call a PHP library function and so it happens that most of those functions are written in C and exposed through PHP. On the other hand php_session_create_id does not have an equivalent exposed to PHP, since that one is used internally by PHP when you start a session using PHP session api.

Shinnok
  • 6,279
  • 6
  • 31
  • 44