Jump to content

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

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.