Jump to content

loggedIn() function?


conker87

Recommended Posts

At the moment, when my members login their details get put into session variables, that's fine - works great.

 

However, what annoys me is that whenever I want to validation someone being logged in, I have to use the following in each and every place this happens:

<?php
if (isset($_SESSION['username'], $_SESSION['password']))
     {
      $check = mysql_num_rows(mysql_query("SELECT * FROM `members_table` WHERE `username_field` = '".$_SESSION['username']."' AND `password_field` ='".$_SESSION['password']."'"));
       if ($check == 0)
        {
         echo "<title>love v4 // login: error</title><h2>Invalid Username/Password.</h2>";
        }
       else if ($check == 1)
        {
         
        }
     }
?>

 

I'm wondering if I could make this into some sort of function to go into my global functions page. Such as the following:

 

<?php
function loveLogged()
{
  if (isset($_SESSION['username'], $_SESSION['password']))
     {
      $check = mysql_num_rows(mysql_query("SELECT * FROM `members_table` WHERE `username_field` = '".$_SESSION['username']."' AND `password_field` ='".$_SESSION['password']."'"));
       if ($check == 0)
        {
         return false;
        }
       else if ($check == 1)
        {
         return true;
        }
     }
}
?>

 

With the code in the page being:

 

<?php
if (loveLogged())
{
  //Do true stuff.
}
?>

 

Would this work, and if not, how would I change it for it to work?

Link to comment
https://forums.phpfreaks.com/topic/55628-loggedin-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.