[SOLVED] Using $_SESSION[] variable

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
Janco
Forum Commoner
Posts: 37
Joined: Fri Apr 25, 2008 2:51 am

[SOLVED] Using $_SESSION[] variable

Post by Janco »

Hi,

I'm trying to display a link to a certain page if a specific user is logged in but I can't seem to get it working.

I have done this in my LOGIN PAGE:

Code: Select all

$query1="select uname,passwd from cks_users where uname = '$lname' and passwd = '$lpass'";
$result1=mysql_query($query1) or die ("Could not log in because". mysql_error());
 
$query2="select uname,passwd from login where uname = '$lname' and passwd = '$lpass'";
$result2=mysql_query($query2) or die ("Could not log in because". mysql_error());
 
$query1="select uname,passwd from cks_users where uname = '$lname' and passwd = '$lpass'";
$result1=mysql_query($query1) or die ("Could not log in because". mysql_error());
 
$query2="select uname,passwd from login where uname = '$lname' and passwd = '$lpass'";
$result2=mysql_query($query2) or die ("Could not log in because". mysql_error());
 
if (mysql_num_rows($result1) > 0) {
                $_SESSION['basic_is_logged_in'] = true;
                $_SESSION['logged_in'] = $lname;
                $_SESSION['cks'] = true;
                mysql_query("update cks_users set last_login=SYSDATE() where uname='$lname'") or die (mysql_error());
                mysql_query("insert into user_audit(uname,user_action,date_time) values('$lname','LOGIN',SYSDATE())");
                header('Location: cksmain.php');
} elseif (mysql_num_rows($result2) > 0) {
        $_SESSION['basic_is_logged_in'] = true;
        $_SESSION['logged_in'] = $lname;
        mysql_query("update login set last_login=SYSDATE() where uname='$lname'") or die (mysql_error());
        mysql_query("insert into user_audit(uname,user_action,date_time) values('$lname','LOGIN',SYSDATE())");
        header('Location: main.php');
}
 else {
        echo "<center><h4><font color=\"red\">Incorrect Username or Password</font></h4></center>";
}
 
The _SESSION['logged_in'] = $lname; works nice on all pages but the following just does not want to work no matter what I try:

attempt 1:

Code: Select all

 
<?php if (!$_SESSION['cks']) {} else { echo "<a href=\"cksmain.php\">Back to CKS Main</a>"; } ?>
attempt 2:

Code: Select all

 
<?php if ($_SESSION['cks'] = true) { echo "<a href=\"cksmain.php\">Back to CKS Main</a>"; } ?>
attempt 3:

Code: Select all

 
<?php if ($_SESSION['cks'] = true) { echo "<a href=\"cksmain.php\">Back to CKS Main</a>"; } else {} ?>
One of two things happen, either the link is displayed throughout or it's not displayed at all. If search around for answers but couldn't find anything that relates to what I'm trying.

Help will be greatly appreciated.
Last edited by Janco on Tue May 27, 2008 1:08 am, edited 1 time in total.
Janco
Forum Commoner
Posts: 37
Joined: Fri Apr 25, 2008 2:51 am

Re: Using $_SESSION[] variable to add a link if a specific user

Post by Janco »

Sorry guys, my problem existed between my computer and my chair!

I just had to add a value to $_SESSION['cks'] i.e.

Code: Select all

$_SESSION['cks'] = 1
and then do something like

Code: Select all

 
<?php
$cks = $_SESSION['cks'];
 
if ($cks !== 1) {} else { echo "<a href=\"cksmain.php\">Back to CKS Main</a>"; } ?></br>
<?php echo $cks; ?>
 
The above works but is there a better way of doing it?
Post Reply