search for in the  
<getmyinodegetmyuid>
Last updated: Thu, 19 May 2005

getmypid

(PHP 3, PHP 4, PHP 5)

getmypid -- Gets PHP's process ID

Description

int getmypid ( void )

Returns the current PHP process ID, or FALSE on error.

Warning

Process IDs are not unique, thus they are a weak entropy source. We recommend against relying on pids in security-dependent contexts.

See also getmygid(), getmyuid(), get_current_user(), getmyinode(), and getlastmod().



User Contributed Notes
getmypid
Pure-PHP
21-Mar-2005 04:26
You can use this function also to avoid more than one instance of your app.

You can also use this class.
http://www.pure-php.de/node/20

Usage:

<?php

inlude
("ProcessHandler.class.php");

if(
ProcessHandler::isActive()){
   die(
"Already running!\n";);
}else{
  
ProcessHandler::activate();
  
//run my app
}

?>
brooke at jump dot net
24-Oct-2003 07:49
One good use for this is deciding on a concurrency-safe temporary file or directory name. You can be assured that no two processes on the same server have the same PID, so this is enough to avoid collisions. For example:

$tmpfile = "/tmp/foo_".getmypid();
// Use $tmpfile...
// Use $tmpfile...
// Use $tmpfile...
unlink ($tmpfile);

If you are sharing /tmp over the network (which is odd....) then you can, of course, mix in the PHP server's IP address.
barry at staes dot nl
29-Jul-2003 02:26
Why dont use use uniqid() to get a unique serial nr for security purposes?
Or md5(uniqid()), for extra fun.
Check http://www.php.net/uniqid for more about this..
Webmaster at PopCart dot com
26-Mar-2003 06:29
$_SERVER['REMOTE_ADDR'] is a bad thing to use these days.
Since AOL, and MSN Browsers change the IP address at the
most uncomfortable times, such as going from unsecure to
secure mode, you cannot rely on it staying the same anymore.
Using this Octal-Quad for hanging on to a user, such as in
shopping carts will ALWAYS FAIL with AOL browsers.  FYI.
10-Sep-2002 04:46
Something you can use to combine several weak entropy sources, into a better init value for srand() and mt_srand():

mt_srand(time()
   ^ (int)microtime()
   ^ ip2long($_SERVER['REMOTE_ADDR'])
   ^ (int)$_SERVER['REMOTE_PORT']
   ^ @getmypid()
   ^ @disk_free_space('/tmp')
   );

Note that getmypid() may return 0 on some shared PHP installations that have excluded it for "safe"...

The safe mode also exclude the disk_free_space(directory) function call in some cases, for security reason. This explains the use of the @ operator to ignore warnings... the directory should be a highly active directory, which is accessible and typically used for temporary contents generated by PHP.

The above code combines all these sources using XOR binary operations, to avoid 32-bit overflows.
carl at NOSPAM dot thep dot lu dot se
05-Aug-2001 06:07
As it is very unlikely today that there will be two PHP processes with
the same process ID within the same microsecond, this function can
be used for something like srand((int)microtime() ^ getmypid() ^ time())
to have a decent chance of avioding a collision.
<p>
Just relying on the time in microseconds is a rather Bad Thing to do, since
a contect switch might very well take a lot less than that, so there's a decent
chance of two simultaneous request to get the same ID.
<P>
If you're running your php script upon an HTTP request, you might even want to toss in the IP addy of the remote host in your seed for a bit of extra entropy. You can't be too paranoid when it comes to 'random' numbers.
<p>(Side note: I tried using hardware to generate random numbers, but
with my setup it took just about forever to gather enough entropy.
All hail ran3()! )

<getmyinodegetmyuid>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT