Page 1 of 1

script works, but doesnt work :S

Posted: Mon Mar 01, 2004 11:24 am
by malcolmboston
ok, here some background on what the script is supposed to do

i have also built the registration form successfully fyi

basically here we have a login script, its designed to well log people in, when a persons credentials have been verified against mysql its supposed to set some $var's and then redirect (header location way) unfortunately the script is not setting the var whatsoever, i tried changing the url it passes to just to make sure my ID was correct and it is so technically the $vars should get set

heres the code

Code: Select all

<?php
require ("dbhandler.php");
require ("datehandler.php");
// lets turn the posted vars into better looking vars
$loginusername = $_POST["login_username"];
$loginpassword = $_POST["login_password"];

// now lets strip just in case of formatting mistakes
$loginusername = stripslashes($loginusername);
$loginpassword = stripslashes($loginpassword);

// check that both variables have been set
if
((!$loginusername) || (!$loginpassword))
{
	header("Location: ../index.php");
	exit();
}

//now lets turn the password into MD5 format
$md5pass = md5($loginpassword);

// now we have converted and formatted all the vars lets run a MYSQL query
$loginquery = mysql_query("SELECT username FROM members WHERE username = '$loginusername' AND password = '$md5pass'");

// now lets see if theres a match already in the database
$loginauth = mysql_num_rows($loginquery);
if
(($loginauth > 0))
{ 
	
	// time to update the last login date for this user
	$updatelastlogin = mysql_query ("UPDATE members SET last_login=now() WHERE username='$loginusername'") or die(mysql_error());
	$logged_in = "yes";
	$hellousername = "$loginusername";
	// and now set the users status as logged in
	// now to set some variables so its perfectly clear the user is logged in
	// this user has been verified and given access
	header("Location: ../index.php");
}
else
{
	// this user cannot be found in the database
	unset($loginusername);
	unset($loginpassword);
	header("Location: ../index.php"); 
	
}
exit();
?>
now the login form is on the index page and i am therefore basically redirecting to that page, i dont know if this is the problem (cant see any good reason why it would be)

i also thought maybe it was a header problem but alas, it isnt, i also thought it could be that i you cannot set a var whilst redirecting but ive done it before so it cant be

also to check on the index page i written this script

Code: Select all

<?php
if(isset($hellousername))
{
print "You are logged In";
}
else
{
print "nope, not working";
}
?>
still it keeps tellin me that it aint working when my ID is correct

any ideas peeps?

Posted: Mon Mar 01, 2004 11:45 am
by malcolmboston
oh yeah, thought i shuld point out that indx.php DOES have <?php session_start(); ?> in it

Posted: Mon Mar 01, 2004 12:13 pm
by malcolmboston
:bump: hmmmmm, no-one got any ideas? :bump:

Posted: Mon Mar 01, 2004 12:18 pm
by Illusionist
having session_start(0 on your pages will make no differences because your not using sessions. But i would advise you to do so!

Posted: Mon Mar 01, 2004 12:19 pm
by malcolmboston
ok fine

but why arent he $vars passing i dont understand this at all

Posted: Mon Mar 01, 2004 12:19 pm
by Weirdan
malcolmboston wrote:oh yeah, thought i shuld point out that indx.php DOES have <?php session_start(); ?> in it
But the code you presented to us does not ;) And you have no [php_man]session_register[/php_man] nor $_SESSION superglobal array element assignments in it.

Posted: Mon Mar 01, 2004 12:20 pm
by malcolmboston
darn so thats teh reason it aint passing,

god i knew it was sometihng simple, <-- has just proved hes still a nub @ heart

cheers guys, so that would solve my probs?

Posted: Mon Mar 01, 2004 12:22 pm
by toms100
i presume

Code: Select all

<?php
   $logged_in = "yes";
   $hellousername = "$loginusername"; 
?>
are both the session variables you want to use.
do this:

Code: Select all

<?php
   $_SESSION['logged_in'] = "yes";  /// I suggest you change this to a boolean value, ie, either 1 or 0. that way you can do if($_SESSION['logged_in']), which will be much easier in the long run!
   $_SESSION['hellousername'] = $loginusername; 
?>
then:

Code: Select all

<?php
if(isset($_SESSION['hellousername']))
{
print "You are logged In";
}
else
{
print "nope, not working";
} 
?>

Posted: Mon Mar 01, 2004 12:28 pm
by malcolmboston
well yeah ill try and explain a little better

when a user is verified based upon the $var existing i would print some conditional HTML such as add comment etc
so

Code: Select all

// something like
if (!isset($_SESSION['hellousername']))
{
print "you are not logged in<br>please login or regiseter
<a class="interaction" ref="register.php">here</a>";
}
else
{
print "Welcome Back $_SESSION['hellousername']";
}
?>
thats not what im gonna be doing but i cant be bothered writing out the whole script,

thanks for helping me on that oversight anyway guys