Current location: Hot Scripts Forums » Programming Languages » PHP » No Multiple Login For User


No Multiple Login For User

Reply
  #1 (permalink)  
Old 03-24-09, 06:42 AM
x70sef x70sef is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
No Multiple Login For User

i have the following script which lets users log in to the website.

PHP Code:

<?php  

session_start
();   
include 
'dbc.php';  
$tbl_name="members"// Table name   

// Connect to server and select databse.  
mysql_connect("$host""$username""$password")or die("cannot connect");   
mysql_select_db("$db_name")or die("cannot select DB");  

// username and password sent from form   
$myusername=$_POST['myusername'];   
$mypassword=$_POST['mypassword'];   

// To protect MySQL injection (more detail about MySQL injection)  
$myusername stripslashes($myusername);  
$mypassword stripslashes($mypassword);  
$myusername mysql_real_escape_string($myusername);  
$mypassword mysql_real_escape_string($mypassword);  

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";  
$result=mysql_query($sql);  

if (
$row mysql_fetch_array($result)) 

// this sets variables in the session   
        
$_SESSION['user'] = $myusername;   

         
        if (
$row['mem_type'] == 'Student'
        { 
            
header("location:login_success.php");    
            exit ; 
        } 
        elseif (
$row['mem_type'] == 'Teacher'
        { 
            
header("location:controlpanel.php");    
            exit ; 
        } 
         
}  
else {
echo 
"<font color=#005B99><font face=Calibri><center><h2>Wrong Username or Password </h2><br/>";  
echo 
"<h3>To re-enter username and password click <a href=\"main_login.php\">here</a></h3></center></font>";  
}  
?>
the problem is that the user can log on as many times as they like..

i want a message sayin ' username is already logged onto website ' how do i change the script to achieve this??

thanks in advance..
Reply With Quote
  #2 (permalink)  
Old 03-24-09, 07:58 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Well you could do it like this:
PHP Code:

<?php
session_start
();
if(empty(
$_SESSION["user"])&&empty($_SESSION["password"]))
{
 
$_SESSION["user"] = empty($_POST['myusername']) ? "" mysql_real_escape_string(stripslashes($_POST['myusername']));
 
$_SESSION["password"] = empty($_POST['mypassword']) ? "" mysql_real_escape_string(stripslashes($_POST['mypassword']));
 include 
'dbc.php';
 
$tbl_name="members";
 
mysql_connect("$host""$username""$password")or die("cannot connect");
 
mysql_select_db("$db_name")or die("cannot select DB");
 
$sql="SELECT * FROM $tbl_name WHERE username='".$_SESSION["user"]."' and password='".$_SESSION["password"]."'";
 
$result=mysql_query($sql);
 if(
$row mysql_fetch_array($result))
 {
  if(
$row['mem_type'] == 'Student')
  {
   
header("location:login_success.php");
   exit;
   }
  elseif(
$row['mem_type'] == 'Teacher')
  {
   
header("location:controlpanel.php");
   exit ;
   }
  }
 else
 {
  echo 
"<font color=#005B99><font face=Calibri><center><h2>Wrong Username or Password </h2><br/>";
  echo 
"<h3>To re-enter username and password click <a href=\"main_login.php\">here</a></h3></center></font>";
  }
 }
else
{
 echo 
$_SESSION["user"]." is already logged onto website.";
 }
?>
Or maybe like this:
PHP Code:

<?php
session_start
();
if(empty(
$_SESSION["user"])&&empty($_SESSION["password"]))
{
 
$_SESSION["user"] = empty($_POST['myusername']) ? "" mysql_real_escape_string(stripslashes($_POST['myusername']));
 
$_SESSION["password"] = empty($_POST['mypassword']) ? "" mysql_real_escape_string(stripslashes($_POST['mypassword']));
 include 
'dbc.php';
 
$tbl_name="members";
 
mysql_connect("$host""$username""$password")or die("cannot connect");
 
mysql_select_db("$db_name")or die("cannot select DB");
 
$sql="SELECT * FROM $tbl_name WHERE username='".$_SESSION["user"]."' and password='".$_SESSION["password"]."'";
 
$result=mysql_query($sql);
 if(
$row mysql_fetch_array($result))
 {
  if(
$row['mem_type'] == 'Student')
  {
   
$_SESSION["student"] = "location:login_success.php";
   }
  elseif(
$row['mem_type'] == 'Teacher')
  {
   
$_SESSION["teacher"] = "location:controlpanel.php";
   }
  }
 else
 {
  echo 
"<font color=#005B99><font face=Calibri><center><h2>Wrong Username or Password </h2><br/>";
  echo 
"<h3>To re-enter username and password click <a href=\"main_login.php\">here</a></h3></center></font>";
  }
 }
if(!empty(
$_SESSION["student"])){header($_SESSION["student"]);exit;}
elseif(!empty(
$_SESSION["teacher"])){header($_SESSION["teacher"]);exit;}
else{echo 
"You do not have permission to access this page";exit;}
?>
__________________
Jerry Broughton
Reply With Quote
  #3 (permalink)  
Old 03-24-09, 10:25 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
Can I ask why you want to do this? (just for my own curiosity)

I've never really had a problem with this. If the site is well designed then it won't do any harm having multiple logins. Admittedly there's few reasons why a user should be logged in several times though...
__________________
Always sanitise your data


Best regards
Reply With Quote
  #4 (permalink)  
Old 03-24-09, 01:06 PM
x70sef x70sef is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
i want it so a user cannot log into the website twice at the same time under the same username and password
Reply With Quote
  #5 (permalink)  
Old 03-24-09, 06:11 PM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
Yeah, but why? What difference would it make?

Don't take that the wrong way. I'm just curious.
__________________
Always sanitise your data


Best regards
Reply With Quote
  #6 (permalink)  
Old 03-25-09, 07:53 AM
x70sef x70sef is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
i want it so a user cannot log into the website twice at the same time under the same username and password
Reply With Quote
  #7 (permalink)  
Old 03-25-09, 09:26 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
oh never mind.
__________________
Always sanitise your data


Best regards
Reply With Quote
  #8 (permalink)  
Old 03-26-09, 02:49 PM
x70sef x70sef is offline
Newbie Coder
 
Join Date: Feb 2009
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
im not taking it the wrong way.. it makes the website much more secure
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Login Protection Request zearus1 Script Requests 0 06-02-06 08:59 PM
Help with multiple user login and redirect Spotted_Doe New Members & Introductions 3 01-30-06 12:12 AM
one login script for multiple websites tori Script Requests 1 12-28-05 07:16 PM
single login using multiple user tables jersey Script Requests 0 09-16-05 08:32 PM
Combine Multiple Login Scripts fernandose PHP 2 02-04-05 04:09 PM


All times are GMT -5. The time now is 08:31 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.