简体   繁体   English

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

[英]PHP - Query changes my session variables

The code below works once. 下面的代码只能运行一次。

When the page loads, the data is displayed correctly. 页面加载后,数据将正确显示。 Hoever, When I refresh the page, all my session variables change to reflect the data pulled from the sql query below. 但是,当我刷新页面时,所有会话变量都会更改以反映从下面的sql查询中提取的数据。 It only happens when I run the query and display the session variables on the same page. 仅当我运行查询并在同一页面上显示会话变量时,才会发生这种情况。 If I have one without the other it doesn't occur. 如果我有一个没有另一个,那就不会发生。 i have been looking for a solution for hours but I cannot wrap my head around this. 我一直在寻找解决方案几个小时,但我无法解决这个问题。 I am pretty new to SQL and PHP so forgive my ignorance. 我对SQL和PHP还是很陌生,所以请原谅我的无知。 Any guidance is GREATLY appreciated. 非常感谢任何指导。

EDIT More Info.. This code is loaded via jQuery. 编辑更多信息。此代码通过jQuery加载。 I first started out with one query on the table clients to populate #chapter1-accountInfo . 我首先从对表clients一个查询开始,以填充#chapter1-accountInfo I later wanted to show all the subBrokers enrolled under the primary account and added the query below to populate .listTable . 后来我想显示所有在主帐户下注册的子经纪人,并在下面添加查询以填充.listTable Whenever both were done it fudged up my session variables. 每当两者都完成时,它就会弄乱我的会话变量。 I tried to experiement by assigning the fields for #chapter1-accountInfo to session variables temporarily to see if only running one query on the table clients would fix my problem. 我尝试通过将#chapter1-accountInfo的字段临时分配给会话变量来进行实验 ,以查看是否仅在表clients上运行一个查询就能解决我的问题。 It didn't. 没有。 That is why I am declaring so many session variables below. 这就是为什么我在下面声明了这么多会话变量的原因。

PHP brokerAccountInfo.php 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> " ;

I think it will help if I shared my other code, 我认为如果我共享其他代码会有所帮助,

JS - This is the load function I'm using JS-这是我正在使用的加载函数

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

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

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

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

PHP - This is the initial setup of the sessions ( again, i am not planning on keeping this many session variables ) 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 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>

So I fixed it.. I think... 所以我修好了..我想...

I still don't know exctly why the variables were changing. 我仍然不太清楚为什么变量会发生变化。

It started working when I messing with the variable names in my brokerSubBrokersTable.php . 当我弄乱我的brokerSubBrokersTable.php的变量名时,它开始工作。 Some variables in this file are populated by data queried from the table as one of the $_SESSION[''] variables does that are set in checkLogin.php 该文件中的某些变量由表中查询的数据填充,就像$_SESSION['']变量之一在checkLogin.php中设置的checkLogin.php

My working code: 我的工作代码:

jQuery 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 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 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 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 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() ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM