AlexzanderFlores AlexzanderFlores - 1 year ago 58
PHP Question

PHP function call stopping page from loading unless it's refreshed

I'm running into a problem when calling a PHP function from my HTML code. My PHP code is mainly connecting to my Java server and requesting information about Twitter accounts. I'm requesting to get their profile picture's URL and their Twitter handle. This information is being displayed on the website.

PHP: (Very top of my .php file that I'm working in)

if(!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header('Location: ../account/login.php?loggedin=no');
} else {

class Functions {
public static function getName() {
if(!isset($_SESSION['twittername'])) {
include '../client.php';
$_SESSION['twittername'] = Client::send("gettwittername:-1", null, -1);
return $_SESSION['twittername'];

public static function getPFP() {
if(!isset($_SESSION['twitterpic'])) {
include '../client.php';
$_SESSION['twitterpic'] = Client::send("gettwitterpic", null, -1);
return $_SESSION['twitterpic'];

HTML: (In the same file as the PHP code above)

<div class="logcontainer">
<div class="profilepic">
<img src="<?php echo Functions::getPFP(); ?>" style="margin: 0px 10px 0px 0px;">
<li class="loggedin"><b><span style="color: #AFAFAF;">CURRENTLY LOGGED IN AS</span>
<span style="color: #5d9dfd; font-size: 14px;"><?php $name = Functions::getName(); echo "<a href='" . $name . "' target='_blank'>" . $name . "</a>"; ?></span>

When running this code it displays my profile picture fine. The server also logs that it received the "gettwitterpic" command. That's all good, however a few lines down when asking to get the name it seems like the function is never called. I never get anything logged on my server and it stops loading the rest of the page. Whenever I refresh the page it loads the Twitter name perfectly fine, but on the first time loading the page it doesn't. Any ideas on how to make this always work, even on the initial page load? Also I'm sure there's better ways to write some of this code, I'm new to web development. How could I improve this functionality in general? Thank you for reading

Answer Source

Both functions will include the client.php file.

In the first page load:

The function getPFP will check that $_SESSION['twitterpic'] is not set, then include the client.php file.

Then the function getName will verify that the $_SESSION['twittername'] is also not set and try to include the client.php file, again, which is your issue.

On the second page load:

The function getPFP will check that $_SESSION['twitterpic'] is set, and will not try to include the client.php file.

Then the function getName will verify that the $_SESSION['twittername'] is not set and include the client.php file, this time without problems because it has been included only once.

Make changes to your logic that gives your solution on that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download