Jump to content

PHP Session Writing to Database


Go to solution Solved by mac_gyver,

Recommended Posts

I'm struggling to get PHP to write sessions to a database. My issue is that when I override the session handler and proceed to call session_start() I get an error message telling me that the headers were already sent. I've been reading for days with no results.

 

Common issues that pop up are the BOM and whitespace. I used vi to double check if I have a BOM, which I don't. As for whitespace, I don't see any in my code. However, for testing purposes I removed all blank lines and all tabs. I'll add formatting back in once I solve the issue, but for now, I removed it all. I don't think whitespace is my issue.

 

The other reason I don't think whitespace is my issue is reading the error message from phpdbg.

[PHP Warning:  session_start(): Cannot send session cookie - headers already sent in /usr/local/apache2/htdocs/index.php on line 43]
[PHP Stack trace:]
[PHP   1. {main}() /usr/local/apache2/htdocs/index.php:0]
[PHP   2. session_start() /usr/local/apache2/htdocs/index.php:43]
[PHP Warning:  session_start(): Cannot send session cache limiter - headers already sent in /usr/local/apache2/htdocs/index.php on line 43]
[PHP Stack trace:]
[PHP   1. {main}() /usr/local/apache2/htdocs/index.php:0]
[PHP   2. session_start() /usr/local/apache2/htdocs/index.php:43]

If I turn on verbose mode in phpdbg the line before the error is:

[L4           0xb73454f4 ZEND_RETURN                    C0                   <unused>             <unused>             /usr/local/apache2/htdocs/index.php]

Based on the code I provide below, line 4 is in the open function and is the return value. Line 43 is session_start(). This is where I'm confused. The error message is telling me calling session_start assigns the header and the open function is also trying to assign a header?

 

From what I understand, the session handler's first call is to the open function. Open is being called, I just don't get why both open and session_start are trying to send headers separately. I don't see any whitespace, there is no BOM. I must be doing something wrong with the session handler?

<?php
$db = pg_connect ( "host='' port='' dbname = '' user='' password=''" );
function open() {
return true;
}
function close() {
return true;
}
function read($id) {
$stmt = 'SELECT data FROM sessions WHERE id = \'' . $id . '\'';
$result = pg_query($stmt);
$numrows = pg_num_rows($result);
if( $numrows > 0 ) {
$row = pg_fetch_array($result, 0, PGSQL_ASSOC);
return $row['data'];
} else
return '';
}
function write($id, $data) {
$access = time();
$stmt = 'REPLACE INTO sessions VALUES (\'' . $id . '\', ' . $access . ',\'' . $data . '\')';
$result = pg_query($stmt);
if( $result)
return true;
return false;
}
function destroy($id) {
$stmt = 'DELETE FROM sessions WHERE id = \'' . $id . '\'';
$result = pg_query($stmt);
if( $result )
return true;
return false;
}
function gc($max) {
$old = time() - $max;
$stmt = 'DELETE * FROM sessions WHERE access < ' . $old;
$result = pg_query($stmt);
if( $result )
return true;
return false;
}
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
session_start();
?>

Note, I'm also attaching the file with the code as well.

index.php

Link to comment
https://forums.phpfreaks.com/topic/297553-php-session-writing-to-database/
Share on other sites

1) REPLACE INTO is not PostgreSQL compatible. I modified the code to instead use:

		$stmt = 'SELECT data FROM sessions WHERE id = \'' . $id . '\'';
		$result = pg_query($stmt);

		$numrows = pg_num_rows($result);
		if( $numrows > 0 ) {
			$stmt = 'UPDATE sessions SET (id, access, data) = (\'' . $id . '\', ' . $access . ',\'' . $data . '\')';
		}
		else {
			$stmt = 'INSERT INTO sessions (id, access, data) VALUES (\'' . $id . '\', ' . $access . ',\'' . $data . '\')';
		}

2) My session now works in Firefox, but phpdbg still complains about the headers. I never tried it in browser because I was focused on getting rid of errors in phpdbg before hand. I verified it's working by going into the database and seeing the test session variable I added increment as I refresh.

 

3) This now begs the question, what is phpdbg doing? Why is phpdbg sending headers where my browsers, both Chrome and Firefox, do not? Can I fix/configure phpdbg to work properly?

 

Here is how I am running phpdbg. 

cd /usr/local/apache2/htdocs
phpdbg
phpdbg> exec index.php
phpdbg> run

I imaigne there is an issue with how I am running phpdbg?

  • Solution

yes, your errors are due to the debugger. php code debuggers generally work by adding a layer of output buffing to send the debugging information to their client module.

 

you should generally only use a debugger to debug why your code isn't doing what you expect (that's why they are called debuggers), not as the primary method of running your code.

I started in the browser but moved to using phpdbg once I started having errors show up. Once I was in phpdbg I didn't plan to move out until I removed all errors. So I started with quite a few errors and had dwinled it down to just the remaining errors with the headers. Not knowing any better, I should have gone back to the browser once I was down to just the headers.

 

I will definitely know better for next time. I am new to PHP and didn't know that the debugger was adding an extra layer and modifying/sending headers.

 

Thank you for clearing that up.

hello sir, 

actually i have started session in a page and i want to know if i login from index.php it take me to home.php(where i have started a session ) if i give a url index.php it redirects me to that page but i want to use session which doesnot redirect me to that page unless until i dont logout from that page 

try to help me please 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.