PHP - 查询更改我的会话变量

时间:2015-08-30 01:27:36

标签: php variables session

以下代码可以使用一次。

加载页面时,数据会正确显示。但是,当我刷新页面时,我的所有会话变量都会发生变化,以反映从下面的sql查询中提取的数据。它只在我运行查询并在同一页面上显示会话变量时才会发生。如果我有一个没有另一个,它不会发生。我一直在寻找一个解决方案几个小时,但我无法解决这个问题。我是SQL和PHP的新手,请原谅我的无知。任何指导都非常感谢。

编辑更多信息.. 这段代码是通过jQuery加载的。我首先在表clients上开始使用一个查询来填充#chapter1-accountInfo。我后来想要显示在主帐户下注册的所有子代理商,并添加了下面的查询以填充.listTable。每当两者都完成时,它就会捏造我的会话变量。我尝试体验,将#chapter1-accountInfo的字段分配给会话变量暂时,看看是否只在表clients上运行一个查询就可以修复我的问题问题。它没有。这就是我在下面声明这么多会话变量的原因。

PHP brokerAccountInfo.php

<?php session_start() ;

include ( 'database/sql_link.php' ) ;

$ID          = $_SESSION[ 'loginID'    ] ;
$companyName = $_SESSION[ 'company'    ] ;
$authority   = $_SESSION[ 'userLevel'  ] ;
$realName    = $_SESSION[ 'actualName' ] ;
$userName    = $_SESSION[ 'userName'   ] ;
$contact     = $_SESSION[ 'email'      ] ;

echo "

    <h2 class='chapterTitle'>
        Account Details
    </h2>

    <div class='chapter1' id='chapter1-accountInfo'>

        <p class='floatLeft'> Company Name: </p>
        <p class='floatRight'> $companyName </p>

        <br clear='both'>

        <p class='floatLeft'> Active Since: </p>
        <p class='floatRight'> $contact </p>

        <br clear='both'>

        <p class='floatLeft'> Your Account Level: </p>
        <p class='floatRight'> $authority </p>

        <br clear='both'>

        <p class='floatLeft'> Your Name: </p>
        <p class='floatRight'> $realName </p>

    </div> " ;

$query  = "    SELECT     *
               FROM       clients
               WHERE      companyName = '$companyName'
               AND        userLevel   = 'subBroker'
          " ;


$result = $db -> query( $query ) ;

if ( !$result = $db -> query($query) ) {
    die( ' There was an error running the query [ ' . $db -> error . ' ] ' ) ;
}

echo "
       <h2 class='chapterTitle'> Your Sub-Accounts </h2>

       <table  id='brokerSubBrokersTable'>
       <thead>
           <tr>
               <th> Company    </th>
               <th> Name       </th>
               <th> Login Name </th>
               <th> Email      </th>
           </tr>
       </thead>
       <tbody>
     " ;

while( $row = $result -> fetch_assoc() ) {

    $company     = $row[ 'companyName' ] ;
    $actualName  = $row[ 'actualName'  ] ;
    $loginName   = $row[ 'userName'    ] ;
    $email       = $row[ 'eMail'       ] ;

    echo "  <tr>
                 <td>  $company    </td>
                 <td>  $actualName </td>
                 <td>  $loginName  </td>
                 <td>  $email      </td>
            </tr> " ;
}
echo " </tbody>
       </table> " ;

我认为如果我分享其他代码会有所帮助,

JS - 这是我正在使用的加载功能

$( '#navBrokeAccount' ).on( 'click' , function() {

        $( '#content' ).fadeOut( 'fast' ).hide() ;
        $( '#content' ).load( 'brokerAccount.php' , function() {

            $( '.chapter1' ).load( 'brokerAccountInfo.php' ) ;

        } ).delay( 500 ).fadeIn( 'slow' ) ;
 } ) ;

PHP - 这是会话的初始设置(同样,我不打算保留这么多会话变量)

<?php session_start() ;



if( !function_exists( 'hash_equals' ) )
{

    function hash_equals( $a , $b )
    {

        $ret   = strlen( $a ) ^ strlen( $b ) ;
        $ret  |= array_sum( unpack( "C*" , $a ^ $b ) ) ;

        return !$ret ;

    }

}




include( 'database/sql_link.php' ) ;

$user    = mysqli_real_escape_string( $db , $_GET[ 'uName' ] ) ;
$pass    = mysqli_real_escape_string( $db , $_GET[ 'pWord' ] ) ;
$query   = "
             SELECT  *
             FROM    clients
             WHERE   userName = '$user'
           " ;

$result = $db -> query( $query ) ;

if ( mysqli_num_rows( $result ) == 0 )
{

    echo "error" ;

}

else
{

    while ( $row  = $result -> fetch_assoc() ) {

        $userName    = $row[ 'userName'    ] ;
        $hash        = $row[ 'Pass_Word'   ] ;
        $companyName = $row[ 'companyName' ] ;
        $realName    = $row[ 'actualName'  ] ;
        $id          = $row[ 'ID'          ] ;
        $firstLogin  = $row[ 'firstLogin'  ] ;
        $userLevel   = $row[ 'userLevel'   ] ;
        $email       = $row[ 'eMail'       ] ;

    }

    if ( hash_equals( $hash , crypt( $pass , $hash ) ) ) {

        $_SESSION[ 'loginID'    ] = $id          ;
        $_SESSION[ 'company'    ] = $companyName ;
        $_SESSION[ 'userLevel'  ] = $userLevel   ;
        $_SESSION[ 'email'      ] = $email       ;
        $_SESSION[ 'actualName' ] = $realName    ;

        if( isset( $_SESSION[ 'loginID' ] ) )
        {
            echo $_SESSION[ 'loginID' ] ;
        }
        else
        {
            echo 'Session Error' ;
        }

    } else {

        echo 'Invalid' ;

    }


$result         -> free() ;
$db             -> close() ;

PHP brokerAccount.php

<div class='contentHeader'>

    <h1 class='contentTitle'> Your Account </h1>

</div>

<br clear='both'>

<div class='chapter1'> 

    <!-- THIS IS THE DIV THE DATA IS BEING LOADED INTO  -->    

</div>

1 个答案:

答案 0 :(得分:0)

所以我修好了..我想......

我仍然不知道为什么变量会发生变化。

当我在brokerSubBrokersTable.php中弄乱变量名时,它开始工作了。此文件中的某些变量由表中查询的数据填充为$_SESSION['']中设置的checkLogin.php变量之一

我的工作代码:

<强>的jQuery

    $( '#content').load( 'brokerAccount.php' , function() {
        $.getJSON( 'brokerAccountInfo.php' , function( info ) {

            var companyInf = ( info.companyInf ) ;
            var nameInf    = ( info.nameInf    ) ;
            var levelInf   = ( info.levelInf   ) ;
            var activeInf  = ( info.activeInf  ) ;

            $ ( '#chapter1-accountInfo-company' ).text( companyInf ) ;
            $ ( '#chapter1-accountInfo-name'    ).text( nameInf    ) ;
            $ ( '#chapter1-accountInfo-level'   ).text( levelInf   ) ;
            $ ( '#chapter1-accountInfo-active'  ).text( activeInf  ) ;

            // This query is where the initial problem was.  Changed some variable names and it started working . .
            //
            $( '#chapter1-subBrokersList' ).load( 'brokerSubBrokersTable.php' ) ;

        } ) ; // End JSON function . .

    } ) ; // End .load function . .

<强> brokerAccount.php

<div class='chapter1'>
    <h2 class='chapterTitle'> Account Details </h2>
    <div class='chapter1-accountInfo' id='chapter1-accountInfo'>
           <p class='floatLeft'> Company Name: </p>
           <p class='floatRight' id='chapter1-accountInfo-company'> </p>
           <br clear='both'>
           <p class='floatLeft'> Name: </p>
           <p class='floatRight' id='chapter1-accountInfo-name'>    </p>
           <br clear='both'>
           <p class='floatLeft'> Privilege: </p>
           <p class='floatRight' id='chapter1-accountInfo-level'>   </p>
           <br clear='both'>
           <p class='floatLeft'> Active Since: </p>
           <p class='floatRight' id='chapter1-accountInfo-active'>  </p>
    </div>

    <!-- THIS IS BEING LOADED AS A SEPARATE QUERY -->
    <div id='chapter1-subBrokersList'>  </div>

</div>

<强> brokerAccountInfo.php

<?php session_start() ;
include( 'database/sql_link.php' ) ;
$loginID  =   $_SESSION[ 'loginID' ] ;
$query    = "
                SELECT     *
                FROM       clients
                WHERE      ID ='$loginID'
             " ;
$result = $db -> query( $query ) ;
if ( !$result = $db -> query($query) ) {
    die( ' There was an error running the query [ ' . $db -> error . ' ] ' ) ;
}
while( $row = $result -> fetch_assoc() ) {
    $companyID   = $row[      'companyName' ] ;
    $realName    = $row[      'actualName'  ] ;
    $authority   = $row[      'userLevel'   ] ;
    $active      = $_SESSION[ 'Timestamp'   ] ;
    $return_data = array( "companyInf"  => $companyID ,
                          "nameInf"     => $realName  ,
                          "levelInf"    => $authority ,
                          "activeInf"   => $active    ) ;
    header( 'Content-Type: application/json' ) ;
    echo json_encode( $return_data ) ;
    exit() ;
}
$db          -> close() ;
$query       -> free()  ;
$result      -> free()  ;

<强> brokerSubBrokersTable.php

<?php session_start() ;
include ( 'database/sql_link.php' ) ;

$levelID     =           "subBroker"  ;
$userComp    = $_SESSION[ 'company' ] ;
$query = " SELECT    *
           FROM      clients
           WHERE     companyName  =  '$userComp'
           AND       userLevel    =  '$levelID'
           ORDER BY  ID " ;

      $result = $db -> query( $query ) ;

if ( !$result = $db -> query( $query ) ) {
    die( ' There was an error running the query [ ' . $db -> error . ' ] ' ) ;
} else if ( mysqli_num_rows( $result ) == 0 ) {
    echo "Error running query or no results returned..." ;
} else {
    echo "
            <h2 class='chapterTitle'> Your Sub-Accounts </h2>
            <table  id='brokerSubBrokersTable'  class='listTable'  style=\"padding:5px;\" >
                <thead>
                    <tr>
                        <th> Company    </th>
                        <th> Name       </th>
                        <th> Login Name </th>
                        <th> Email      </th>
                    </tr>
                </thead>
                <tbody>
    " ;
while( $row = $result -> fetch_assoc() ) {
$rowID       = $row[ 'ID'          ] ;
$companyName = $row[ 'companyName' ] ;
$nameID      = $row[ 'actualName'  ] ;
$loginName   = $row[ 'userName'    ] ;
$userEmail   = $row[ 'eMail'       ] ;
    echo "
                    <tr style=\"height:10px;\" >
                        <td style=\" padding-left:10px; text-align:center; \" >  $companyName </td>
                        <td style=\" padding-left:10px; text-align:center; \" >
                            <p class='quoteLink'>
                                <span class='icon-profile'>  $nameID </span>
                            </p>
                        </td>
                        <td style=\" padding:0 10px; text-align:center; \" > $loginName       </td>
                        <td style=\" padding:0 10px; text-align:center; \" > $userEmail       </td>
                    </tr>
    " ;

    }
    echo "
                </tbody>
            </table>
    " ;


}


$db     -> close() ;
$result -> free()  ;

<强> checkLogin.php

<?php session_start() ;

if( !function_exists( 'hash_equals' ) ) {
    function hash_equals( $a , $b ) {
        $ret   = strlen( $a ) ^ strlen( $b ) ;
        $ret  |= array_sum( unpack( "C*" , $a ^ $b ) ) ;
        return !$ret ;
    }
}
include( 'database/sql_link.php' ) ;

$user    = mysqli_real_escape_string( $db , $_GET[ 'uName' ] ) ;
$pass    = mysqli_real_escape_string( $db , $_GET[ 'pWord' ] ) ;
$query   = "
             SELECT  *
             FROM    clients
             WHERE   userName = '$user'
           " ;
$result = $db -> query( $query ) ;

if ( mysqli_num_rows( $result ) == 0 )
{
    echo "error" ;
} else {
    while ( $row  = $result -> fetch_assoc() ) {
        $userName    = $row[ 'userName'    ] ;
        $hash        = $row[ 'Pass_Word'   ] ;
        $companyName = $row[ 'companyName' ] ;
        $realName    = $row[ 'actualName'  ] ;
        $id          = $row[ 'ID'          ] ;
        $firstLogin  = $row[ 'firstLogin'  ] ;
        $userLevel   = $row[ 'userLevel'   ] ;
        $email       = $row[ 'eMail'       ] ;
    }
    if ( hash_equals( $hash , crypt( $pass , $hash ) ) ) {
        $_SESSION[ 'loginID'    ] = $id          ;
        $_SESSION[ 'company'    ] = $companyName ;
        $_SESSION[ 'userLevel'  ] = $userLevel   ;

        if( isset( $_SESSION[ 'loginID' ] ) ) {
            echo $_SESSION[ 'loginID' ] ;
        } else {
            echo 'Session Error' ;
        }
    } else {
        echo 'Invalid' ;
    }
$result         -> free() ;
$db             -> close() ;