Jump to content

Help with session_is_registered


AlMch

Recommended Posts

Hi, 

I'm trying to make an old php script (won't work on anything over 5.3) run on php7. I'm not a coder by any means but get along by adapting examples I find on the web. I'm doing OK for the most part but am stuck finding what to replace session_is_registered with.

This is what it is currently: 

$pref = mysql_query("SELECT * FROM preferences WHERE ID = '0'",$connection)
or die(mysql_error());
$pdata = mysql_fetch_array($pref);
mysql_free_result($pref);

// I've already changed the above to:
// 
// pref = mysqli_query($connection,"SELECT * FROM preferences WHERE ID = '0'")
// or die(mysqli_error($connection));
// $pdata = mysqli_fetch_array($pref);
// mysqli_free_result($pref);
//
// Just wanted to show you the whole section in its orignal state.

// It's this line below that I'm not sure about...

if(!session_is_registered('defaultseasonid_') || !session_is_registered('defaultmatchtypeid_') || !session_is_registered('defaultlanguage_'))
	
{
	$_SESSION['defaultseasonid_'] = $pdata['DefaultSeasonID'];
	$_SESSION['defaultmatchtypeid_'] = $pdata['DefaultMatchTypeID'];
	$_SESSION['defaultlanguage_'] = $pdata['DefaultLanguage'];
	$defaultseasonid = $_SESSION['defaultseasonid_'];
	$defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'];
	$defaultlanguage = $_SESSION['defaultlanguage_'];
}
else
{
	$defaultseasonid = $_SESSION['defaultseasonid_'];
	$defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'];
	$defaultlanguage = $_SESSION['defaultlanguage_'];
}

session_unset();

Any pointers anybody can give me would be much appreciated.

TIA

Link to comment
Share on other sites

And I've also tried that: 

if(isset($SESSION['defaultseasonid_']) || ($SESSION['defaultmatchtypeid_']) || ($SESSION['defaultlanguage_']))

But it doesn't seem to be pulling those default values into the session. PhpStorm says all the syntax is ok but it doesn't pull the required default values into the session. 

Edited by AlMch
Link to comment
Share on other sites

As of PHP 7.0 you could replace this code...

if(!session_is_registered('defaultseasonid_') || !session_is_registered('defaultmatchtypeid_') || !session_is_registered('defaultlanguage_'))
    
{
    $_SESSION['defaultseasonid_'] = $pdata['DefaultSeasonID'];
    $_SESSION['defaultmatchtypeid_'] = $pdata['DefaultMatchTypeID'];
    $_SESSION['defaultlanguage_'] = $pdata['DefaultLanguage'];
    $defaultseasonid = $_SESSION['defaultseasonid_'];
    $defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'];
    $defaultlanguage = $_SESSION['defaultlanguage_'];
}
else
{
    $defaultseasonid = $_SESSION['defaultseasonid_'];
    $defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'];
    $defaultlanguage = $_SESSION['defaultlanguage_'];
}

with ...

    $defaultseasonid = $_SESSION['defaultseasonid_'] ?? $pdata['DefaultSeasonID'];
    $defaultmatchtypeid = $_SESSION['defaultmatchtypeid_'] ?? $pdata['DefaultMatchTypeID'];
    $defaultlanguage = $_SESSION['defaultlanguage_'] ?? $pdata['DefaultLanguage'];

"??" is the "null coalesce" operator, so

$a = $b ?? $c ?? $d;

will set $a to the first of the three values that is not  null.

  • Great Answer 1
Link to comment
Share on other sites

Yep session_start is called. Looks like I missed out a ! so I now have this...

if(!isset($_SESSION['defaultseasonid_']) || ($_SESSION['defaultmatchtypeid_']) || ($_SESSION['defaultlanguage_']))

So, and excuse my ignorance, but I think that's saying use the defaultseasonid_ from the database if there isn't one already in the session? Now we're getting somewhere as this is working and bringing in a list of players for the current (default) season. However, a little later on in the script I have a couple of drop-downs to select from the list of available seasons and another with a list of matchtypes. Both these worked prior to adding the ! but now only the season drop-down is working. 

 

EDIT - posted this before seeing your last reply.

Edited by AlMch
Link to comment
Share on other sites

1 minute ago, AlMch said:

now only the season drop-down is working. 

That's the only one that you check with isset(). Should be

if ( !isset($_SESSION['defaultseasonid_']) || !isset($_SESSION['defaultmatchtypeid_']) || !isset($_SESSION['defaultlanguage_']) )

but the ?? method is a lot simpler.

  • Great Answer 1
Link to comment
Share on other sites

3 hours ago, Barand said:

if ( !isset($_SESSION['defaultseasonid_']) || !isset($_SESSION['defaultmatchtypeid_']) || !isset($_SESSION['defaultlanguage_']) )

For the sake of completeness, an alternative way to write this is

if (!isset($_SESSION['defaultseasonid_'], $_SESSION['defaultmatchtypeid_'], $_SESSION['defaultlanguage_']))
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.