login through switch only working for default

Hi

I have a simple 1 page cms for editing some text on small websites. i am adding a multiple user function.
the correct user with it's credentials is chosen through the url:
index.php?user=home

it works correct, except for this part:

$ppp = md5($password);
            define('SMC_PASS', $ppp);

SMC_PASS is for a login for a sitemap page i put in an iframe. i can log in correctly to the sitemap in the iframe with the default or "else" credentials but not with the home or start passwords.
(= only password protected)

why could this be?
the rest works well, i can login to the cms page with all 3 cases.. only the sitemap pass (SMC_PASS) doesn't login for case home and start.

thanks
<?

if(!empty($_GET['user']))
	{
		switch($_GET['user'])
		{
		case "home":
			$username23 = "home"; //
			$password = "home"; //
			define ("USERN", $username23);
		break;
		
		case "start":
			$username23 = "start"; // 
			$password = "start"; //
			define ("USERN", $username23);
		break;
	
		default:
			$usernameadmin = "peps"; //
			$password = "peps"; //
			define ("USERN", $usernameadmin); //
			$username23 = "";
			break;
		}
	}
else 
	{
	$usernameadmin = "peps"; //
	$password = "peps"; //
	define ("USERN", $usernameadmin); //
	$username23 = "";
	}
	
//Site password
	define ("PASSW", $password);
	
	$ppp = md5($password);
		define('SMC_PASS', $ppp);
	?>

Open in new window

peps03Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
Undefined variable is pretty clear information.  You may be looking at an issue of variable scope - inside functions and classes, the variable scope may only apply to the current function and class.
http://www.php.net/manual/en/language.variables.scope.php

You can put these variables into a function's argument list, or set them as properties of a class with a "setter" method.  Or you can add them to the $GLOBALS array.  $GLOBALS is a "superglobal" array - that is, it exists in every scope and namespace.
0
 
peps03Author Commented:
for some reason the case: "home" & "start" password aren't passed to:
define('SMC_PASS', $ppp);

because if i make it like this it will work:
define('SMC_PASS', md5(home));

> now case "home" can log in..

what is wrong with the code??

0
 
Ray PaseurCommented:
Looking at the code sample above, all it appears to do is define some constants or clear some variables.  As a matter of good programming practice, your defined constants should actually be constant -- that is, they should not be set conditionally, like variables.  Since we cannot see how you use the constants and variables, it is impossible to know exactly what is going wrong.

Regarding this statement: define('SMC_PASS', md5(home)); -- you might want to add error_reporting(E_ALL); to the top of all your scripts.  As written that statement will look for a constant named home and attempt to make the md5() string from the value in that constant.  If the constant is not defined, PHP will silently use NULL as input to the MD5() function.  In other words, it will always produce the same md5() string.

If you want to learn about login and logout processes, I have an article here that might be helpful.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html

Best of luck with the project, ~Ray
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
peps03Author Commented:
Hi Ray,

Thanks for the reaction!

i read some stuff about constants and variables on php.net.

I replaced the constants with variables. but now i have the problem that i get an undefined variable 'username' and 'password'

could this be because the variables are in a function and the $username and $password are defined on another page that is included?

how can i fix this?

thanks!
0
 
peps03Author Commented:
Thanks for the directions, i finally figured it out!

Kind regards!
0
 
Ray PaseurCommented:
Thanks for the points -- glad I could help.  All the best, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.