Session problem

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
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Session problem

Post by HiddenS3crets »

Sessions is not registering.

Login page:

Code: Select all

<?php
include("./../top.php");
include("./../navigation.php");

$admin = $_SESSION['admin_set'];

if(empty($admin)) {

    echo "<font><form action=\"login.php\" method=\"POST\"><center>Username:<br><input type=\"text\" name=\"username\"><br>Password:<br><input type=\"password\" name=\"password\"><br><br><input type=\"submit\" value=\"Login\"></form></font>";
} else {

    echo "Logged in";
}
include("./../bottom.php");
?>
Page that logs user in:

Code: Select all

<?php
$user = $_POST['username'];
$pass = $_POST['password'];

if(empty($user)) {

    include("./../top.php");
    include("./../navigation.php");
    echo "<font><center>Please go back and enter an username</center></font>";
    include("./../bottom.php");
    die();
}

if(empty($pass)) {

    include("./../top.php");
    include("./../navigation.php");
    echo "<font><center>Please go back and enter a password</center></font>";
    include("./../bottom.php");
    die();
}

$pass = MD5($pass);
include("./../con.php");

$query = "SELECT * FROM `main_admins` WHERE `username` = '$user' AND `password` = '$pass'";
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);

if($rows == 0) {

    include("./../top.php");
    include("./../navigation.php");
    echo "<font><center>Wrong username or password. Please go back and try again</center></font>";
    include("./../bottom.php");
    die();
} else {

    session_register('admin_set');
    header("location:index.php");
}
?>
The file top.php begins with the required session_start(); Any idea on why session_register('admin_set'); isn't working?
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Is session_start() called on the page that logs the user in? It's necessary for each page if not set in php.ini.

Edit => Ignore that, I see it included in top.php but shouldn't it be included right before session_register?
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

It's simple- session_register('admin_set'); merely sets a *variable* .. I see no where in your code you assign a value to it..

In fact, if you do instead:

Code: Select all

$_SESSION['admin_set'] = true;
you dont even need session_register() function at all- it sets teh variable *and* assigns a value all in one..

In your next page, since admin_set is set as a session array key, but it is *empty* (null) it has nothing to display or do.. :)
Post Reply