DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Can't create sessions (http://www.daniweb.com/forums/thread110999.html)

lordx78 Feb 26th, 2008 10:44 am
Can't create sessions
 
admin.php
<?php
// connection to MySQL server
mysql_connect('localhost','root','');
mysql_select_db('administration');


if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $loginPassword=$_POST['password'];
  $MM_redirectLoginSuccess = "validated.php";
  $MM_redirectLoginFailed = "admin.php";
  $MM_redirecttoReferrer = true;
 
  $errors = array();
 
        if(empty($_POST['username'])) {
                $errors[] = 'You think whom going to fill up the USERNAME for you?';
        }       
        if(empty($_POST['password'])) {
                $errors[] = 'You think whom going to fill up the PASSWORD for you?';
        }       
        if (empty($errors)) {
       
  $loginUsername = get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername);
  $password = get_magic_quotes_gpc() ? $password : addslashes($password);
  $LoginRS_query = "SELECT username, password FROM adminprofile WHERE username='$loginUsername' AND password='$loginPassword'";
  $LoginRS = mysql_query($LoginRS_query) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
 
  if ($loginFoundUser) {
   
        session_name ('YourVisitID');
        session_start();
        $_SESSION['user_id'] = $loginFoundUser[0];
        $_SESSION['first_name'] = $loginFoundUser[1];
        $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
       
    echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
        exit();
        }
  else {
 
    echo "<script type='text/javascript'>location.href='$MM_redirectLoginFailed';</script>";
}
}
mysql_close();
}

?>

validated.php
<?php

session_name ('YourVisitID');
session_start(); // Start the session.
$MM_redirectLoginFailed = "admin.php";
$MM_redirecttoReferrer = true;

// If no session value is present, redirect the user.
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT'])) ) {

        echo "<script type='text/javascript'>location.href='$MM_redirectLoginSuccess';</script>";
        exit(); // Quit the script.
}

?>

I've two pages as displayed above. I wanted to enable sessions so that the user can not go to validated.php straight without any validation. But the the code above does not create any sessions at all. The codes in validated.php is working fine and the codes in admin.php is working fine without the line 32 - 36. This line did not create/store any sessions when executed. Each time, after I log in using the correct username and password, it will redirect me bak to the index.php. Please help.

nav33n Feb 26th, 2008 11:42 am
Re: Can't create sessions
 
in validated.php page, print out the session variables.
print_r($_SESSION); 

lordx78 Feb 26th, 2008 11:49 am
Re: Can't create sessions
 
Where should I place this code, before/after session start? thx

nav33n Feb 26th, 2008 11:50 am
Re: Can't create sessions
 
After session_start();

lordx78 Feb 26th, 2008 11:54 am
Re: Can't create sessions
 
When I paste the code as directed, the page validated.php was blinking, similar to an infinity loop. Like the page try to load, but can't. Finally, I stopped that page. Advise pls.

nav33n Feb 26th, 2008 12:03 pm
Re: Can't create sessions
 
Have 2 pages.
<?php //page1.php
session_start();
$_SESSION['name']="test";
header("location: page2.php");
?>
In page2.php, write this.
<?php
session_start();
print_r($_SESSION);
?>
See if it prints test in page2.

lordx78 Feb 26th, 2008 12:10 pm
Re: Can't create sessions
 
I get output like this:

Array ( [name] => test )

Then.

nav33n Feb 26th, 2008 12:18 pm
Re: Can't create sessions
 
So, session exists.

Redirect to a dummy page, for eg, page2.php and have print_r($_SESSION) in it. Check if session is being passed !

lordx78 Feb 26th, 2008 12:30 pm
Re: Can't create sessions
 
I get output like this:

Array ( )

It can't create the the session rite? But why. All my syntax was correct rite?

nav33n Feb 26th, 2008 12:51 pm
Re: Can't create sessions
 
Strange! Are you sure, $loginFoundUser[0], $loginFoundUser[1], md5($_SERVER['HTTP_USER_AGENT']) have values ? I think nothing is assigned to session variables.


All times are GMT -4. The time now is 2:25 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC