shmop_open
Description
The shmop_open of Shmop for PHP creates or opens the shared memory block.
Syntax
shmop_open( int $key, string $mode, int $permissions, int $size ): Shmop|false
Parameters
key
System's id for the shared memory block. Can be passed as a decimal or hexadecimal.
mode
The flags that you can use:
Flag | Description |
---|---|
a | access (sets SHM_RDONLY for shmat), use this flag when you need to open an existing shared memory segment for read only |
c | create (sets IPC_CREATE), use this flag when you need to create a new shared memory segment or if a segment with the same key exists, try to open it for read and write |
w | read & write, use this flag when you need to read and write to a shared memory segment, use this flag in most cases. |
n | create new (sets IPC_CREATE|IPC_EXCL), use this flag when you want to create a new shared memory segment but if one already exists with the same flag, fail. This is useful for security purposes, using this you can prevent race condition exploits. |
permissions
The permissions that you wish to assign to your memory segment, those are the same as permission for a file. Must be passed as an octal. For example: 0o644 (notation: 0o, user: 6, group: 4, other: 4).
Octal | Description |
---|---|
0 | none |
1 | execute |
2 | write |
3 | write and execute |
4 | read |
5 | read and execute |
6 | read and write |
7 | read and write and execute |
size
The size of the shared memory block you wish to create in bytes.
Return
Returns a Shmop instance to access the shared memory segment on success or false on failure.
Examples
1 · key · number · decimal
<? $key = 1234567890; $mode = "a"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
<!doctype html> <html> <head> <title>PHP</title> <style> html, a { background-color: rgb(255 0 255 / 0.1); } a { color: purple; transition-duration: 1s; &:hover { background-color: rgb(0 0 0 / 0.1); color: black; text-decoration-color: transparent; } } </style> </head> <body> <a href="/php/">PHP</a> <script> function myfunction(myparameter) { const mytarget = myparameter.target; const mystyle = mytarget.style; mystyle.position = "absolute"; mystyle.left = `${Math.random() * (window.innerWidth - mytarget.offsetWidth)}px`; mystyle.top = `${Math.random() * (window.innerHeight - mytarget.offsetHeight)}px`; } document.querySelector("a").addEventListener("mouseout", myfunction); </script> </body> </html>
2 · key · number · hexadecimal
<? $key = 0x0123456789abcdef; $mode = "a"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
object(Shmop)#1 (0) { }
3 · key · function · ftok
<? $filename = __FILE__; $project_id = "t"; $key = ftok($filename, $project_id); $mode = "a"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
<!doctype html> <html> <head> <title>PHP</title> <style> html, a { background-color: rgb(255 0 255 / 0.1); } a { color: purple; transition-duration: 1s; &:hover { background-color: rgb(0 0 0 / 0.1); color: black; text-decoration-color: transparent; } } </style> </head> <body> <a href="/php/">PHP</a> <script> function myfunction(myparameter) { const mytarget = myparameter.target; const mystyle = mytarget.style; mystyle.position = "absolute"; mystyle.left = `${Math.random() * (window.innerWidth - mytarget.offsetWidth)}px`; mystyle.top = `${Math.random() * (window.innerHeight - mytarget.offsetHeight)}px`; } document.querySelector("a").addEventListener("mouseout", myfunction); </script> </body> </html>
4 · key · function · fileinode
<? $filename = __FILE__; $key = fileinode($filename); $mode = "a"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
object(Shmop)#1 (0) { }
5 · mode · a
<? $filename = __FILE__; $project_id = "t"; $key = ftok($filename, $project_id); $mode = "a"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
object(Shmop)#1 (0) { }
6 · mode · c
<? $filename = __FILE__; $project_id = "t"; $key = ftok($filename, $project_id); $mode = "c"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
object(Shmop)#1 (0) { }
7 · mode · w
<? $filename = __FILE__; $project_id = "t"; $key = ftok($filename, $project_id); $mode = "w"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
object(Shmop)#1 (0) { }
8 · mode · n
<? $filename = __FILE__; $project_id = "t"; $key = ftok($filename, $project_id); $mode = "n"; $permissions = 0o644; $size = 16; $return = shmop_open($key, $mode, $permissions, $size); var_dump($return);
bool(false)