PDA

View Full Version : Global variables, sessions (noob help)


ClammyDoo
05-16-2007, 08:22 PM
What I'm trying to do is set up a global variable for when the user logs in to store his user name, and call upon this variable later..

$loginName = $row['Username'];

session_register("username");

$username = $loginName;

This may be wrong?

I also set up a variable for a loginflag

session_register($loginFlag = 1);

This is probably wrong also.

Here I'm trying to display different data to a logged in user and someone who isn't logged in.

<?php if ($loginFlag == 1) { ?>
<p><b>Welcome staff member <br />
<a href="../chooseupdate.php">
<?php echo $loginName; ?> </a>
you're logged in.</b>
<? } else { ?>
<p><b>You are not Logged in!</b> <a href="../login.php">Log in</a> to check your messages.<br />
Do you want to <a href="../login.php">Log in</a> or <a href="../register.php">register</a>?</p> <? { ?>

What am i doing wrong? Appreciate the help

JohnDubya
05-16-2007, 08:24 PM
session_register() is being deprecated. Instead, you should use the superglobal $_SESSION.


$loginName = $row['Username'];
$_SESSION['username'] = $loginName;

//Or more simply...
$_SESSION['username'] = $row['Username'];

You'll also need to start the session at the top of each page that will use $_SESSION variables:

session_start();

ClammyDoo
05-16-2007, 08:46 PM
session_register() is being deprecated. Instead, you should use the superglobal $_SESSION.


$loginName = $row['Username'];
$_SESSION['username'] = $loginName;

//Or more simply...
$_SESSION['username'] = $row['Username'];

You'll also need to start the session at the top of each page that will use $_SESSION variables:

session_start();

i see, thanks for the reply, im pretty new with PHP

to set up the loginflag, it would be $_SESSION['$loginFlag'] = 1;

ja?

JohnDubya
05-16-2007, 08:48 PM
Except without the $.

$_SESSION['loginFlag'] = 1;

And then to use it later, for, let's say, an if statement:


if ($_SESSION['loginFlag'] == 1) {
//do this
}

//etc.

ClammyDoo
05-16-2007, 09:17 PM
Sorry to be a bother, but is there any reason why this isn't working

//Loop Through database to check if username and password are valid
do {
//Checks if username and password are valid
if (($row['Username'] == $_POST['txtusername']) AND ($row['Password'] == $_POST['txtpassword']))
{
//session_register($loginFlag = 1);

$_SESSION['loginFlag'] = 1;
//$_SESSION['username'] = $loginName;
//Or more simply...
$_SESSION['username'] = $row['Username'];

$counter = $counter + 1;

if ($row['AccessLevel'] < 3)
{
$level = 'staff';
}
else
$level = 'member';
}

} while ($row = mysql_fetch_assoc($result));
// If username and password are valid then it redirects the user to cms.php and creates a variable named $valid
if ($_SESSION['loginFlag'] == 1) {

$_SESSION['valid'];

$valid = "YES";
{
if ($level == 'staff')
{
header( 'Location: chooseupdate.php' );
}
else
{
header( 'Location: memberspage.php' );
// if username and password are incorrect it redirects user to login.php
}else

header( 'Location: login.php' ) ;
// Creates variable value to tell PHP that login failed.
$valid = "NO";
}
?>

I get an error "Parse error: parse error, unexpected T_ELSE in C:\wamp\www\checklogin.php on line 58"

Line 58 is bolded up above

JohnDubya
05-16-2007, 09:57 PM
Couple things.

You don't need to use () around each if() condition. You can just do this instead:


if ($row['Username'] == $_POST['txtusername'] AND $row['Password'] == $_POST['txtpassword'])


You also didn't end that if() statement with a closing curly bracket.

I'm not too familiar with do-while loops. Why don't you look into just using a while() loop? Seems like they are much easier. Read up on it, at least.

http://us.php.net/while
http://www.tizag.com/phpT/whileloop.php

And lastly, there are some inappropriately used curly brackets in the while() statement. Always check them to make sure there is an opening and a closing curly bracket:


// If username and password are valid then it redirects the user to cms.php and creates a variable named $valid
if ($_SESSION['loginFlag'] == 1) {

$_SESSION['valid'];

$valid = "YES";
{
//Should be a }


if ($level == 'staff')
{
header( 'Location: chooseupdate.php' );
}
else
{
header( 'Location: memberspage.php' );
// if username and password are incorrect it redirects user to login.php
} else
//There is no opening curly bracket after the else.

header( 'Location: login.php' ) ;
// Creates variable value to tell PHP that login failed.
$valid = "NO";
}

aedrin
05-16-2007, 10:10 PM
I'm not too familiar with do-while loops.

They can be quite useful.

But in this case, they're not the right thing.

The first time the do/while runs, $row will be empty.

Use something like this instead:


while ($row = mysql_fetch_assoc($result)) {
// ... do stuff with $row here ...
}

Fumigator
05-16-2007, 10:13 PM
A "do while()" is the same thing as a "while()" the only difference being the condition of the loop is checked after the code block is executed with the "do while()". (So the code block is always executed at least once.)

In the case of the posted code a "do while()" is entirely inappropriate because the condition of the loop assigns a value to $row which is used inside the loop. That first time in the loop $row won't have any value, unless the value of $row has been assigned prior to the "do while()", in which case the loop is completely unnecessary.

ClammyDoo
05-16-2007, 10:47 PM
I know what you guys are saying, but I'm not quite sure how to change the do while loop ti just a while loop..

ClammyDoo
05-17-2007, 01:26 AM
anyone help me with a while loop#?

Fumigator
05-17-2007, 02:26 AM
see post #7