Class Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Class Problem

Post by evilcoder »

Well, its a function in the class anyways.

heres the function.

Code: Select all

<?php
function StoreSession( $strUserID = 0, $strUserName = "", $intSecurity = 0, $strArray = "" ) 
   { 
      session_start(); 
       
      $Database = new Database(); 
      @$SvrConnection = mysql_connect( $Database->strHost, $Database->strUser, $Database->strPass ); 
       
      if ( $SvrConnection ) 
      { 
         // Connected to the Database OK 
         $DBConnection = mysql_select_db( $Database->strData, $SvrConnection ); 
          
         if ( $DBConnection ) 
         { 
            $FieldNames = explode( ",", $this->__ExtraFieldNames ); 
            $FieldNameValue = explode( ",", $strArray ); 
             
            $strQuery = "INSERT INTO {$this->__StoreSessionTableName} "; 
            $strQuery .= "( '{$this->__FieldNameSession}','{$this->__FieldNameUser}','{$this->__FieldNameSecurity}','{$this->__FieldNameUserID}' "; 
             
            $i = 0; 
            foreach( $FieldNames as $name=>$value ) 
            { 
               if ( $value <> "" ) 
               { 
                  $strQuery .= ",'$value'"; $i++; 
               } 
            } 
            $strQuery .= ") VALUES ('" . session_id() . "','$strUserName','$intSecurity','$strUserID'"; 
             
            $e = 0; 
            foreach( $FieldNameValue as $name=>$value ) 
            { 
               if ( $i > $e ) 
               { 
                  $strQuery .= ",'$value'"; $e++; 
               } 
            } 
            $strQuery .= ")"; 
             
            $result = mysql_query( $strQuery ); 
             
            if ( $result ) 
            { 
               // Connection to Database Failed 
               return true; 
            } 
            else 
            { 
               // Connection to Database Failed 
               return false; 
            } 
         } 
         else 
         { 
            // Connection to Database Failed 
            return false; 
         } 
      } 
      else 
      { 
         // Connection to Database Failed 
         return false; 
      } 
   } 
?>
ok now when i use the login form and enter my username andpass, it says:

Couldnt Start New Session.


This is how its called:

Code: Select all

<?php 
} 

function ProcessLogin() 
{ 
   global $dtlsSecurity; 
    
   $strUser = @$_POST['struName']; 
   $strPass = md5( $_POST['strPass'] ); 
    
   $Database = new Database; 
   @$SvrConnection = mysql_connect( $Database->strHost, $Database->strUser, $Database->strPass ); 
       
   if ( $SvrConnection ) 
   { 
      // Connected to the Database OK 
      $DBConnection = mysql_select_db( $Database->strData, $SvrConnection ); 
          
      if ( $DBConnection ) 
      { 
         $strQuery = "SELECT * FROM users "; 
         $strQuery .= "WHERE username = '$strUser' "; 
         $strQuery .= "AND password = '$strPass' "; 
         $results = mysql_query( $strQuery ); 
         $result = mysql_fetch_row( $results ); 
          
         if ( $result ) 
         { 
            // Write to the log file 
            $strLog = "Login OK: {$result[1]}, {$result[3]}, {$result[4]}, {$_SERVER['REMOTE_ADDR']}, " . date("d/m/Y h:i:m A"); 
            $dtlsSecurity->AddLog( $strLog ); 

            if ( $dtlsSecurity->StoreSession( $result['userid'], $result['username'], $result['level'], "{$result['firstname']},{$result['lastname']}" ) ) 
            { 
?> 
You have now logged in. 

<?php 
            } 
            else 
            { 
?> 
Couldn't start new session:<br><br> 
<a href="javascript:history.go(-1)">Go Back</a> 
<?php 
            } 
         } 
         else 
         { 
            $strLog = "Login Failed: $strUser, $strPass, {$_SERVER['REMOTE_ADDR']}, " . date("d/m/Y h:i:m A"); 
            $dtlsSecurity->AddLog( $strLog ); 

?> 
Login Failed<br><br> 
The login details that you have entered are incorrect. Please use the link below to go 
back and check your username and password again.<br><br> 
<a href="javascript:history.go(-1)">Go Back</a> 

<?php 
         } 
      } 
      else 
      { 
         $strLog = "Database Error: Login, $strUser, $strPass, {$_SERVER['REMOTE_ADDR']}, " . mysql_error() . ", " . date("d/m/Y h:i:m A"); 
         $dtlsSecurity->AddLog( $strLog ); 

?> 
Database Error<br><br> 
<a href="javascript:history.go(-1)">Go Back</a> 
<? 
      } 
   } 
   else 
   { 
      $strLog = "Database Server Error: Login, $strUser, $strPass, {$_SERVER['REMOTE_ADDR']}, " . mysql_error() . ", " . date("d/m/Y h:i:m A"); 
      $dtlsSecurity->AddLog( $strLog ); 

?>

Its wierd because:

Code: Select all

// Write to the log file 
$strLog = "Login OK: &#123;$result&#1111;1]&#125;, &#123;$result&#1111;3]&#125;, &#123;$result&#1111;4]&#125;, &#123;$_SERVER&#1111;'REMOTE_ADDR']&#125;, " . date("d/m/Y h:i:m A"); 
$dtlsSecurity-&gt;AddLog( $strLog );
That actually gets recorded into the log table. Which means the function is working properly but there is something after that which is not creating the session.
?>[/quote]
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

<?php
function StoreSession( $strUserID = 0, $strUserName = "", $intSecurity = 0, $strArray = "" ) 
   &#123; 
      session_start(); 
       
      $Database = new Database(); 
      @$SvrConnection = mysql_connect( $Database->strHost, $Database->strUser, $Database->strPass ); 


etc...
Try starting the session from the page that's calling the function. That might help.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

Nope, tried that before. I really have no clue whats wrong, because my class and its functions are correct
Post Reply