AlMch Posted August 28, 2019 Share Posted August 28, 2019 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 More sharing options...
Barand Posted August 28, 2019 Share Posted August 28, 2019 Try reading the manual (session_is_registered) . After all, that's why they wrote it. Link to comment Share on other sites More sharing options...
AlMch Posted August 28, 2019 Author Share Posted August 28, 2019 9 minutes ago, Barand said: Try reading the manual (session_is_registered) . After all, that's why they wrote it. Yes I've looked at that but as far as I can see, it doesn't actually explain what to use instead. But thanks for your rather curt reply, here's me thinking I'd posted in the PHP Help forum. What a way to make newcomers feel welcome. Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2019 Share Posted August 28, 2019 Well, I could repeat to you what the manual says or you could read it yourself. It seems more efficient to cut out the middle-man. And the page I linked you to answers your question... Link to comment Share on other sites More sharing options...
AlMch Posted August 28, 2019 Author Share Posted August 28, 2019 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. Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2019 Share Posted August 28, 2019 It's $_SESSION. not $SESSION Have you called session_start() at the top of the script? Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2019 Share Posted August 28, 2019 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. Link to comment Share on other sites More sharing options...
AlMch Posted August 28, 2019 Author Share Posted August 28, 2019 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. Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2019 Share Posted August 28, 2019 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. Link to comment Share on other sites More sharing options...
AlMch Posted August 28, 2019 Author Share Posted August 28, 2019 Barand - awesome. The ?? method has worked perfectly. There we go - I've learnt a bit more today, thank you! Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2019 Share Posted August 28, 2019 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.