Jump to content

A most annoying problem


Steppio

Recommended Posts

As much as i want to just give up i absolutely have to finish this as i've started it. Can anybody please help me before i blow my brains out - i have a problem (no pun intended). I want some users to have other rights then other users, for instance i want both types of user (admin and user) to log in through the same form, but i want the login function to take the username and password and depending on the username i want it to decide if the login is for admin or user access. The code i have worked with a switch, but never ever ever works no matter where i put it in the code:

 

function login($usern, $pass)
// check username and password with db
// if yes, return true
// else throw exception
{
// connect to db
$conn = db_connect();

// check username is unique
$result = $conn->query("select * from t_users where username='$usern' and password=sha1('$pass')");
switch ($usern) {
case 'Ste':
          	$bgi = yes;
            break;
          case 'Keith':
          	$bgi = yes;
            break;
          case 'Dave.Fingers':
            $bgi = yes;
            break;
     	case 'Ben':
            $bgi = yes;
            break;
          case 'Matt':
            $bgi = yes;
            break;
          default:
          	$bgi = no;
     }
     return ('$decide = ' . $bgi . ';');
if ($result->num_rows>0)
  return $decide;
  return true;
if (!$result)
	throw new Exception('Could not log you in.');
else 
	throw new Exception('Could not log you in - message 2.');
}

 

This is were i originally tried to put it, then i made a function out of it itself, tried to call the function and stick the $decide into a session variable, then i tried to stick it into the login page:

 

if ($usern && $pass)
// if they have just tried logging in
{
try
{	
switch ($usern) {
		 case 'Ste':
          	$bgi = yes;
            break;
          case 'Keith':
          	$bgi = yes;
            break;
          case 'Dave.Fingers':
            $bgi = yes;
            break;
     		 case 'Ben':
            $bgi = yes;
            break;
          case 'Matt':
            $bgi = yes;
            break;
          default:
          	$bgi = no;
     }
     return ('$decide = ' . $bgi . ';');
	login($usern, $pass);
	// if they are in the database register the user id
  $_SESSION['valid_user'] = $usern;
}
catch(Exception $e)
{
	require('head.inc');
	// unsuccesful login
	echo 'You could not be logged in. You must be logged in to view this page.<br>';
	echo '<a href="main.php">Back to main page...</a>';
	require('foot.inc');
	exit;
}
require('head.inc');
echo 'Hi ' . $usern . ' and welcome to the Members page. From this page you can control your settings associated with Kin-Kade.com. <br /><br />';
echo '<a href="main.php?<?php echo strip_tags(SID); ?>">Back to main page...</a><br /><br />';
echo $_SESSION['valid_user'];
echo $decide;
require('foot.inc');
}
}
?>

 

None of it works and it never prints the variable $decide, presumably because i have done something wrong that my stupid eyes and overlooking brain cant see. Please, Plllleeeeeaaaase help me.

Link to comment
https://forums.phpfreaks.com/topic/49304-a-most-annoying-problem/
Share on other sites

You can't use multiple returns in succession. As soon as the first return is processed in a function the processing exits the function. Also, you are returning a string in one instance of '$decide = ' . $bgi . ';' What are you expecting that to do? It' won't assing the value to the variable $decide. Also, is there a reason you are using Exceptions to handle functionality?

 

As far as admin vs. user a better way to handle it would be to add a column to the user table for admin. Set the value to 1 for admin users and 0 for everyone else.

 

Anyway, here is a rewrite of your code (not tested) which should have the functionality you are looking for:

 

<?php
function login($usern, $pass)
// check username and password with db
// if yes, return true
// else throw exception
{
 // connect to db
 $conn = db_connect();

 // check username is unique
 $result = $conn->query("select * from t_users where username='$usern' and password=sha1('$pass')");

 if ($result->num_rows===1) {
   return true;
 }

 if (!$result->num_rows<1) {
   return 'That username/password was not found.';
 } elseif ($result->num_rows>1) {
   return 'Unable to log you in. Duplicate records.';
 }

 return 'Unable to log you in. Unknown error';

}
?>

 

<?php

if ($usern && $pass)
// if they have just tried logging in
{
 $login = login($usern, $pass);

 if ($login !== true) {

   // unsuccesful login
   require('head.inc');
   echo 'You could not be logged in. You must be logged in to view this page.<br><br>';
   echo $login; //Login error message
   echo '<br><br><a href="main.php">Back to main page...</a>';
   require('foot.inc');
   exit;

 } else {

   switch ($usern) {
     case 'Ste':
     case 'Keith':
     case 'Dave.Fingers':
     case 'Ben':
     case 'Matt':
       $bgi = yes;
       break;
     default:
       $bgi = no;
    }

   $_SESSION['valid_user'] = $usern;

   require('head.inc');
   echo 'Hi ' . $usern . ' and welcome to the Members page. From this page you can control your settings associated with Kin-Kade.com. <br /><br />';
   echo '<a href="main.php?<?php echo strip_tags(SID); ?>">Back to main page...</a><br /><br />';
   echo $_SESSION['valid_user'];
   echo "<br><br>Admin: $bgi<br><br>";
   require('foot.inc');
 }

}
?>

mjdamato ... i love you. Thank you so much for your help it appears to work excellently. Just in case you think i'm trying to get by on other peoples knowledge i'll explain a little of my predicament. I'm in a band and in the middle of a cash flow crisis i told the band i'd do the site. To begin with i started with a static HTML / CSS combo, but slowly the lure of PHP and Javascript, and basically the whole dynamic website scene began to cloud my focus, and now even though i could pay for someone to finish it off properly i've spent weeks and months doing this whole thing, everything from the graphics to the coding, and i feel as though i absolutely have to finish it. I've realised i'm crap at PHP but i may pursue it further, it's just very complicated and it's the first language of its type i've tried to learn. I've bit off more than i can chew but thank you for your help and everybody who replied, its most appreciated. Thanks again!

 

Ste

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.