script works, but doesnt work :S

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

script works, but doesnt work :S

Post 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?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

oh yeah, thought i shuld point out that indx.php DOES have <?php session_start(); ?> in it
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

:bump: hmmmmm, no-one got any ideas? :bump:
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ok fine

but why arent he $vars passing i dont understand this at all
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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?
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post 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";
} 
?>
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
Post Reply