Jump to content

Db Sessions


yrstruly

Recommended Posts

HiI have a poblem with this code. It dont seem to work. The following error message is displayed: Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 74Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 74Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 75Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 76Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 35Here follows thw code:<?php # Script 3.1 - db_sessions.inc.php /* * This page creates the functional * interface for storing session data * in a database. * This page also starts the session. */ // Global variable used for the database // connections in all session functions: $sdbc = NULL; // Define the open_session() function: // This function takes no arguments. // This function should open the database connection. function open_session() { global $sdbc; // Connect to the database. $sdbc = mysqli_connect ('localhost', 'root', 'musica', 'test') OR die ('Cannot connectto the database.'); return true; } // End of open_session() function. // Define the close_session() function: // This function takes no arguments. // This function closes the database connection. function close_session() { global $sdbc; return mysqli_close($sdbc); } // End of close_session() function. // Define the read_session() function: // This function takes one argument: the session ID. // This function retrieves the session data. function read_session($sid) { global $sdbc; // Query the database: $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc,$sid)); $r = mysqli_query($sdbc, $q); // Retrieve the results: if (mysqli_num_rows($r) == 1) { list($data) = mysqli_fetch_array($r, MYSQLI_NUM); // Return the data: return $data; } else { // Return an empty string. return ''; } } // End of read_session() function. // Define the write_session() function: // This function takes two arguments: // the session ID and the session data. function write_session($sid, $data) { global $sdbc; // Store in the database: $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")',mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data)); $r = mysqli_query($sdbc, $q); return mysqli_affected_rows($sdbc); } // End of write_session() function. // Define the destroy_session() function: // This function takes one argument: the session ID. function destroy_session($sid) { global $sdbc; // Delete from the database: $q = sprintf('DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid)); $r = mysqli_query($sdbc, $q); // Clear the $_SESSION array: $_SESSION = array(); return mysqli_affected_rows($sdbc); } // End of destroy_session() function. // Define the clean_session() function: // This function takes one argument: a value in seconds. function clean_session($expire) { global $sdbc; // Delete old sessions: $q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) <NOW()', (int) $expire); $r = mysqli_query($sdbc, $q); return mysqli_affected_rows($sdbc); } // End of clean_session() function. # **************************** # # ***** END OF FUNCTIONS ***** # # **************************** # // Declare the functions to use: session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session','destroy_session', 'clean_session'); // Make whatever other changes to the session settings. // Start the session: session_start(); ?>

Link to comment
Share on other sites

It's not running the open_session function. Are you running this page stand-alone, or is it included? If it's stand-alone, check to see if the session is being auto-started, if it is this code won't work. To check that, create this page:<?phpphpinfo();?>And then open it in a browser. Scroll down to the area where you see session information, and check the value of session.auto_start.

Link to comment
Share on other sites

It's not running the open_session function. Are you running this page stand-alone, or is it included? If it's stand-alone, check to see if the session is being auto-started, if it is this code won't work. To check that, create this page:<?phpphpinfo();?>And then open it in a browser. Scroll down to the area where you see session information, and check the value of session.auto_start.
Hi should i just create an php page with the php code in it and then run it?
Link to comment
Share on other sites

HiI have a poblem with this code. It dont seem to work. The following error message is displayed: Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 74Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 74Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 75Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 76Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 35Here follows thw code:<?php # Script 3.1 - db_sessions.inc.phpHiThis si what i get: session.auto_start Off Off /* * This page creates the functional * interface for storing session data * in a database. * This page also starts the session. */ // Global variable used for the database // connections in all session functions: $sdbc = NULL; // Define the open_session() function: // This function takes no arguments. // This function should open the database connection. function open_session() { global $sdbc; // Connect to the database. $sdbc = mysqli_connect ('localhost', 'root', 'musica', 'test') OR die ('Cannot connectto the database.'); return true; } // End of open_session() function. // Define the close_session() function: // This function takes no arguments. // This function closes the database connection. function close_session() { global $sdbc; return mysqli_close($sdbc); } // End of close_session() function. // Define the read_session() function: // This function takes one argument: the session ID. // This function retrieves the session data. function read_session($sid) { global $sdbc; // Query the database: $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc,$sid)); $r = mysqli_query($sdbc, $q); // Retrieve the results: if (mysqli_num_rows($r) == 1) { list($data) = mysqli_fetch_array($r, MYSQLI_NUM); // Return the data: return $data; } else { // Return an empty string. return ''; } } // End of read_session() function. // Define the write_session() function: // This function takes two arguments: // the session ID and the session data. function write_session($sid, $data) { global $sdbc; // Store in the database: $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")',mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data)); $r = mysqli_query($sdbc, $q); return mysqli_affected_rows($sdbc); } // End of write_session() function. // Define the destroy_session() function: // This function takes one argument: the session ID. function destroy_session($sid) { global $sdbc; // Delete from the database: $q = sprintf('DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid)); $r = mysqli_query($sdbc, $q); // Clear the $_SESSION array: $_SESSION = array(); return mysqli_affected_rows($sdbc); } // End of destroy_session() function. // Define the clean_session() function: // This function takes one argument: a value in seconds. function clean_session($expire) { global $sdbc; // Delete old sessions: $q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) <NOW()', (int) $expire); $r = mysqli_query($sdbc, $q); return mysqli_affected_rows($sdbc); } // End of clean_session() function. # **************************** # # ***** END OF FUNCTIONS ***** # # **************************** # // Declare the functions to use: session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session','destroy_session', 'clean_session'); // Make whatever other changes to the session settings. // Start the session: session_start(); ?>
Link to comment
Share on other sites

You didn't answer if you were running this standalone or included. The file is named db_sessions.inc.php, are you including this file from somewhere else?
I believe its a stand alone code!THANK YOU.
Link to comment
Share on other sites

Using session_start should call open_session automatically, assuming the session hasn't already been started. You can just call open_session explicitly though, probably after session_start. It won't hurt anything to run it more than once.

Link to comment
Share on other sites

make sure you call open_session() before you call read_session() or the other functions as php reports $sdbc being null.calling open_session() sets $sdbc to a mysqli resource.
Hi can you just exlain,should open_session be before read_session? Canyou guys just explain in full to me please? I quite dont understand.
Link to comment
Share on other sites

HiI have done what you said and nothing is happening, its still giving me that error!Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 73Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 73Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 74Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 75Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\exercise\db_sessions.inc.php on line 34<?php # Script 3.1 - db_sessions.inc.php /* * This page creates the functional * interface for storing session data * in a database. * This page also starts the session. */ // Global variable used for the database // connections in all session functions: $sdbc = NULL; // Define the open_session() function: // This function takes no arguments. // This function should open the database connection. function open_session() { global $sdbc; // Connect to the database. $sdbc = mysqli_connect ('localhost', 'root', 'musica', 'test') OR die ('Cannot connectto the database.'); return true; } // End of open_session() function. // Define the close_session() function: // This function takes no arguments. // This function closes the database connection. function close_session() { global $sdbc; return mysqli_close($sdbc); } // End of close_session() function. // Define the read_session() function: // This function takes one argument: the session ID. // This function retrieves the session data. function read_session($sid) { global $sdbc; // Query the database: $q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc,$sid)); $r = mysqli_query($sdbc, $q); // Retrieve the results: if (mysqli_num_rows($r) == 1) { list($data) = mysqli_fetch_array($r, MYSQLI_NUM); // Return the data: return $data; } else { // Return an empty string. return ''; } } // End of read_session() function. // Define the write_session() function: // This function takes two arguments: // the session ID and the session data. function write_session($sid, $data) { global $sdbc; // Store in the database: $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")',mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data)); $r = mysqli_query($sdbc, $q); return mysqli_affected_rows($sdbc); } // End of write_session() function. // Define the destroy_session() function: // This function takes one argument: the session ID. function destroy_session($sid) { global $sdbc; // Delete from the database: $q = sprintf('DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc, $sid)); $r = mysqli_query($sdbc, $q); // Clear the $_SESSION array: $_SESSION = array(); return mysqli_affected_rows($sdbc); } // End of destroy_session() function. // Define the clean_session() function: // This function takes one argument: a value in seconds. function clean_session($expire) { global $sdbc; // Delete old sessions: $q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) <NOW()', (int) $expire); $r = mysqli_query($sdbc, $q); return mysqli_affected_rows($sdbc); } // End of clean_session() function. # **************************** # # ***** END OF FUNCTIONS ***** # # **************************** # // Declare the functions to use: session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session','destroy_session', 'clean_session'); // Make whatever other changes to the session settings. // Start the session: session_start(); open_session(); ?>

Link to comment
Share on other sites

Reverse the order, do open_session before session_start.This shouldn't be happening though, it doesn't make sense that this would be a problem. There's another database session handler here with an explanation:http://w3schools.invisionzone.com/index.php?showtopic=9731
I tried the re-ordering and nothing happend!
Link to comment
Share on other sites

You can try moving the database connection out of the session open function. I'm not sure why it doesn't want to run that function, but apparently it's not running it before trying to use the other session functions.

$sdbc = NULL;// Connect to the database.$sdbc = mysqli_connect ('localhost', 'root', 'musica', 'test') OR die ('Cannot connectto the database.');// Define the open_session() function:// This function takes no arguments.// This function should open the database connection.function open_session() {return true;} // End of open_session() function

Link to comment
Share on other sites

You can try moving the database connection out of the session open function. I'm not sure why it doesn't want to run that function, but apparently it's not running it before trying to use the other session functions.
$sdbc = NULL;// Connect to the database.$sdbc = mysqli_connect ('localhost', 'root', 'musica', 'test') OR die ('Cannot connectto the database.');// Define the open_session() function:// This function takes no arguments.// This function should open the database connection.function open_session() {return true;} // End of open_session() function

Are you saying i should remove the above code from the complete code,as u put it above?
Link to comment
Share on other sites

Check your original code, the difference between that and the code I posted is that I moved the mysqli_connect line out of the function, so that it always connects to the database regardless of whether or not the open_session function runs. The open_session function now doesn't do anything, it just returns true. The open_session function is the only function that needs to change.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...