Jump to content
Larry Ullman's Book Forums

Alex Peguero

Members
  • Posts

    2
  • Joined

  • Last visited

Posts posted by Alex Peguero

  1. Repost again so that its easier to reasd

    <?php
     
    //this is a global variable that will connect to all sessions.
    
    $dbc = NULL;
    
    function open_session() {
     
     global $dbc;
    
    //Set the database access information as constants:
    DEFINE ('DB_HOST', 'localhost');
    DEFINE ('DB_USER', 'root');
    DEFINE ('DB_PASSWORD', '1515');
    DEFINE ('DB_NAME', 'day2');
    
    //make the connection:
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('did not connect' . mysqli_connect_error());
    
     
    //set the character set:
    mysqli_set_charset($dbc, 'utf8');
    
    //Create the function for escaping and trimming form data
    //Takes one argument: the data to be treated(string)
    //return the treated data(string)
    function escape_data($data, $dbc) {
    
    	//strip the slashes if Magic Quotes is on:
    	if (get_magic_quotes_gpc()) $data = stripslashes($data);
    
    	//apply trim() and mysqli_real_escape_string():
    	return mysqli_real_escape_string($dbc, trim($data));
    }// end of function escape_data()
    
     return true;
     
    }// end of open_sesssion() function
    
    
    // define the close_session() function
    //this function takes no arguments 
    //this function closes the database connection 
    //this function returns the close status. 
    function close_session() {
        global $dbc;
        
        return mysqli_close($dbc);
    } // End of close_session() function.
    
    //define the read_session() function
    //this function takes one argument: the session ID:
    //this function retrieves the session data
    //this function returns the sesion data as a string
    
    function read_session($sid) {
    
    	global $dbc;
    
    	$q= sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($dbc, $sid));
    	$r= mysqli_query($dbc, $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;
    	} 
     
    }//end of read_session() function
    
    //define the write_session() function:
    //this function takes 2 arguments
    //the session ID and  session datat
    
    function write_session($sid, $data) {
        global $dbc;
    
        // Store in the database:
        $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")', mysqli_real_escape_string($sid, $dbc), mysqli_real_escape_string($data, $dbc)); 
        $r = mysqli_query($dbc, $q);
    
    	return true;
    } // End of write_session() function.
    
    //define the destroy_session() function:
    //this function takes 1 argument: the session ID
    
    function destroy_session($sid) {
    
    	global $dbc;
    
    	$q= sprintf('DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($dbc,$sid));
    	$r= mysqli_query($dbc, $q);
    
    	//clear the session array
    	$_SESSION = array();
    
    	return true;
    
    }//end of destroy_session() function 
    
    //define the clean_session() function
    //this function takes one argument: a value in seconds.
    
    function clean_session($expire) { // this is the garbage collection. can wipe out old sessions that weren't formally destroyed.
    	global $dbc;
    
    	//delete old sessions:
    	$q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()', (int) $expire);
    	$r = mysqli_query($dbc, $q);
    
    	return true;
    }// 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');
    
    //session_start() not need because of changes made in php.ini.file
    session_start();
    //omit the PHP tag to avoid 'header already sent' errors!
    
    
  2. Hi I recent combined both "mysql.inc.php" for Effortless Ecommerce and Advance PHP. When I run the script (typing it in the browser of course)  it returns this:

     

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\day2\mysql.inc.php on line 94

    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\day2\mysql.inc.php on line 94

    Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\day2\mysql.inc.php on line 95

    Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\day2\mysql.inc.php on line 58

     

    Now when I go into to the php.ini.file (xampp) and I set "session.auto=1" (zero "0" is default) it works. I want to know why it doesn't work when I leave it on default. I've combined db_sessions.inc.php  from PHP Adance OOP and mysql.inc.php from Effortless. 

     

    myscript:

    ===========================================

     

    <?php
     
    //this is a global variable that will connect to all sessions.
     
    $dbc = NULL;
     
    function open_session() {
     
     global $dbc;
     
    //Set the database access information as constants:
    DEFINE ('DB_HOST', 'localhost');
    DEFINE ('DB_USER', 'root');
    DEFINE ('DB_PASSWORD', 'password ');
    DEFINE ('DB_NAME', 'db');
     
    //make the connection:
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('did not connect' . mysqli_connect_error());
     
     
    //set the character set:
    mysqli_set_charset($dbc, 'utf8');
     
    //Create the function for escaping and trimming form data
    //Takes one argument: the data to be treated(string)
    //return the treated data(string)
    function escape_data($data, $dbc) {
     
    //strip the slashes if Magic Quotes is on:
    if (get_magic_quotes_gpc()) $data = stripslashes($data);
     
    //apply trim() and mysqli_real_escape_string():
    return mysqli_real_escape_string($dbc, trim($data));
    }// end of function escape_data()
     
     return true;
     
    }// end of open_sesssion() function
     
     
    // define the close_session() function
    //this function takes no arguments 
    //this function closes the database connection 
    //this function returns the close status. 
    function close_session() {
        global $dbc;
        
        return mysqli_close($dbc);
    } // End of close_session() function.
     
    //define the read_session() function
    //this function takes one argument: the session ID:
    //this function retrieves the session data
    //this function returns the sesion data as a string
     
    function read_session($sid) {
     
    global $dbc;
     
    $q= sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($dbc, $sid));
    $r= mysqli_query($dbc, $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;
     
    }//end of read_session() function
     
    //define the write_session() function:
    //this function takes 2 arguments
    //the session ID and  session datat
     
    function write_session($sid, $data) {
        global $dbc;
     
        // Store in the database:
        $q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")', mysqli_real_escape_string($sid, $dbc), mysqli_real_escape_string($data, $dbc)); 
        $r = mysqli_query($dbc, $q);
     
    return true;
    } // End of write_session() function.
     
    //define the destroy_session() function:
    //this function takes 1 argument: the session ID
     
    function destroy_session($sid) {
     
    global $dbc;
     
    $q= sprintf('DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($dbc,$sid));
    $r= mysqli_query($dbc, $q);
     
    //clear the session array
    $_SESSION = array();
     
    return true;
     
    }//end of destroy_session() function 
     
    //define the clean_session() function
    //this function takes one argument: a value in seconds.
     
    function clean_session($expire) { // this is the garbage collection. can wipe out old sessions that weren't formally destroyed.
    global $dbc;
     
    //delete old sessions:
    $q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()', (int) $expire);
    $r = mysqli_query($dbc, $q);
     
    return true;
    }// 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');
     
    //session_start() not need because of changes made in php.ini.file
    session_start();
    //omit the PHP tag to avoid 'header already sent' errors!
     
    ============================================
    end of my script 
     
     
    I would like to store the users sessions in a separate table. 
     
×
×
  • Create New...