php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80453 Documentation example error
Submitted: 2020-12-01 12:16 UTC Modified: 2020-12-01 16:35 UTC
From: goran_zarkovic at zoho dot com Assigned: cmb (profile)
Status: Closed Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2020-12-01 12:16 UTC] goran_zarkovic at zoho dot com
Description:
------------
There seems to be an error in serialize() function #1 Example:

<?php
// $session_data contains a multi-dimensional array with session
// information for the current user. We use serialize() to store
// it in a database at the end of the request.

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata)) {
$stmt = odbc_prepare($conn,
"INSERT INTO sessions (id, data) VALUES(?, ?)");
if (!odbc_execute($stmt, $sqldata)) {
/* Something went wrong.. */
}
}
?>

SQL INSERT will result in data and id columns content inverted: $sqldata array contains session_data and id, so positional data binding will have effect of column id containing session_data, and column data containing PHP_AUTH_USER.

Proposed correction:

<?php
// $session_data contains a multi-dimensional array with session
// information for the current user. We use serialize() to store
// it in a database at the end of the request.

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
"UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata)) {
$stmt = odbc_prepare($conn,
"INSERT INTO sessions (id, data) VALUES(?, ?)");
$sqldata = array ($_SERVER['PHP_AUTH_USER'], serialize($session_data) );
if (!odbc_execute($stmt, $sqldata)) {
/* Something went wrong.. */
}
}
?> 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-01 16:19 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: Documentation problem +Package: Variables related -Assigned To: +Assigned To: cmb
 [2020-12-01 16:19 UTC] cmb@php.net
Thanks for reporting in the bugtracker! :)
 [2020-12-01 16:22 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=351822
Log: Fix #80453: Documentation example error

Actually, this example should better be rewritten to something simpler or at
least something relevant.  However, the unserialize() page has the counter
example, so we postpone that, and apply an odd fix instead.
 [2020-12-01 16:24 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=22529a07ac74b0d85326a819eb2f848971c5af48
Log: Fix #80453: Documentation example error
 [2020-12-01 16:24 UTC] phpdocbot@php.net
-Status: Verified +Status: Closed
 [2020-12-01 16:35 UTC] goran_zarkovic at zoho dot com
I must say I'm impressed with your quick response. Not that this error is crucial, but if someone tries to use your example as starting point, at least he/she doesn't have to be confused with unexpected results...
 [2020-12-02 00:25 UTC] mumumu@php.net
Automatic comment from SVN on behalf of mumumu
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=351837
Log: Fix #80453: Documentation example error

Actually, this example should better be rewritten to something simpler or at
least something relevant.  However, the unserialize() page has the counter
example, so we postpone that, and apply an odd fix instead.
 [2020-12-02 00:30 UTC] phpdocbot@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=3e4b77b7b4261631b09a5217e9bf02104a615b76
Log: Fix #80453: Documentation example error
 [2020-12-30 11:58 UTC] nikic@php.net
Automatic comment on behalf of mumumu
Revision: http://git.php.net/?p=doc/ja.git;a=commit;h=c39d57fd8f4d603e8f0a44382cba926eac3de110
Log: Fix #80453: Documentation example error
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat May 15 00:01:23 2021 UTC