简体   繁体   中英

PHP script works online but no offline

This script works online but not offline via WAMP...

function current_user()
    {
        static $current_user;
        if(!$current_user)
        {
            if($_SESSION['userID'])
            {
                $userID = intval($_SESSION['userID']);
                $query = "SELECT * 
                          FROM `users`
                          WHERE `id` = $userID";
                $data  = mysql_query($query);
                if(mysql_num_rows($data))
                {
                    $current_user = mysql_fetch_assoc($data);
                    return $current_user;
                }
            }
        }
        return $current_user;
    }

When this function is called, I get the following error message...

Notice: Undefined index: userID in C:\wamp\www\alpha\_includes\session.php on line 38

Line 38 is $userID = intval($_SESSION['userID']);

The error only occurs when the userID SESSION variable isn't set. That and when the function is called via a page from my offline testing computer (running WAMP).


As a side note, this isn't my script. Credit goes to Jim Hoskins of Think Vitamin Membership.

It's a notice, not an error. To fix:

if (isset($_SESSION['userID']) && $_SESSION['userID'])

You have notices enabled on one server, but not the other. This notice is just telling you that are attempting to read an array element that doesn't exist.

Instead, try this

if(isset($_SESSION['userID']))

change if($_SESSION['userID']) to if(isset($_SESSION['userID'])) first check whether the session variable 'userID' is set before we use it.

Try this code

function current_user()
    {
        static $current_user;
        if(!$current_user)
        {
            if($_SESSION['userID'])
            {
                $userID = intval($_SESSION['userID']);
                $query = "SELECT * 
                          FROM `users`
                          WHERE `id` = $userID";
                $data  = mysql_query($query);
                if(mysql_num_rows($data))
                {
                    $current_user = mysql_fetch_assoc($data);
                    return $current_user;
                }
            }
        }
        return $current_user;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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