print_r怎么不在我的实时服务器上输出任何内容,但在我的本地主机上工作正常?

I'm building a web-application and currently testing out how to retrieve values from the server database and display it on my page. My connection to my database is working just fine.

In my localhost, I run the code print_r($_SESSION['user']; where $_SESSION['user'] stores the user's login session and it works fine. Here's what it outputs on my localhost:

Array([0]=>1 [1]=>Bob [2]=>Dylan [3]=>Bob.Dylan@gmail.com)

where Bob is the user's firstname, Dylan is his lastname, and Bob.Dylan@gmail.com is his email.

So that's all good.

But when it comes to my live server and I run the same print_r($_SESSION['user']; code again it displays absolutely nothing.

I'm eventually working my way to my objective of displaying all the related user's info on the database (basically, their profile page) so I thought I'd start with this and go from there but I can't even get the print_r function to work correctly.

Can anyone help me?

EDIT:

  • My connection to both my localhost and live server database is working fine

My code:

if(isset($_SESSION['user]))
{
     print_r($_SESSION['user']);
}
else
{
     echo "Session is not set" 
}

It's probably falling under the else part. Your session may not have been set correctly.

Try this:

var_dump($_SESSION);  // Check your session variable

if (isset($_SESSION['user'])) {
    print_r($_SESSION['user']);
} else {
    echo "Session is not set"; 
}

So you have syntax errors. It should be:

if(isset($_SESSION['user']))
//                     ^^^
{
     print_r($_SESSION['user']);
}
else
{
     echo "Session is not set";
     //                      ^^^
}

You did:

if(isset($_SESSION['user]))
{
     print_r($_SESSION['user']);
}
else
{
     echo "Session is not set" 
}

Happy Coding!

You could also try the following;

print_r(array_values($_SESSION['user']));

or

var_dump($_SESSION['user]);

According to php manual you probably need to turn on output buffering using ob_start() function.

Please follow the note section from this url: print_r manual