I've been sitting here pondering this for a good 30 minutes now and I just can't see what's wrong.
I've added prints but it just doesn't seem to be contacting the database correctly or not pulling the data from it correctly.
$conf['sql_host'] = 'localhost';
$conf['sql_user'] = 'root';
$conf['sql_pass'] = '';
$conf['sql_data'] = 'c_webauth';
mysql_connect( $conf['sql_host'], $conf['sql_user'], $conf['sql_pass'] ) or die( 'Connection failed: '.mysql_error() );
mysql_select_db( $conf['sql_data'] );
function login( $user, $pass ){
session_regenerate_id();
if ( isset( $_SESSION['user_id'] ) ) {
unset( $_SESSION['user_id'] );
}
$qry = mysql_query( "SELECT user_id, user, user_group, user_name FROM c_users where user='$user' AND pass='".md5($pass)."'" );
if ( mysql_num_rows( $qry ) > 0 ) {
session_regenerate_id();
while ( $data = mysql_fetch_array( $qry ) ) {
$_SESSION['user_id'] = $data['user_id'];
$_SESSION['user_group'] = $data['user_group'];
$_SESSION['user'] = $data['user'];
$_SESSION['user_name'] = $data['user_name'];
// Debug
echo $_SESSION['user_id'];
echo $_SESSION['user_group'];
echo $_SESSION['user'];
echo $_SESSION['user_name'];
session_start( );
}
}
}
login( "username", "password" );
Anychance you can see whats wrong?
Everyone has mentioned that I had my session_start(); in the wrong place, but the echo's print nothing, absolutely no errors anywhere.
You are calling session_start() at the end instead before doing session checks
session_start( );
it should used before session use
You have put session_start( )
at bottom , after you have assigned SESSION variables
Put this function on top of the page and remove from bottom.
You need to put session_start()
at the top of your code, because its at the bottom all $_SESSION
variables are undeclared so they wont return a value. One session_start()
is at the top you'll get the global $_SESSION
variables and you'll also be able to set them. I advise that you create a script called config.php
that has session_start()
and you can include it in all your scripts instead of add session_start()
everytime.
Hope this helps you out :D
<?php
session_start();
$conf['sql_host'] = 'localhost';
$conf['sql_user'] = 'root';
$conf['sql_pass'] = '';
$conf['sql_data'] = 'c_webauth';
mysql_connect( $conf['sql_host'], $conf['sql_user'], $conf['sql_pass'] ) or die( 'Connection failed: '.mysql_error() );
mysql_select_db( $conf['sql_data'] );
function login( $user, $pass ){
session_regenerate_id();
if ( isset( $_SESSION['user_id'] ) ) {
unset( $_SESSION['user_id'] );
}
$qry = mysql_query( "SELECT user_id, user, user_group, user_name FROM c_users where user='$user' AND pass='".md5($pass)."'" );
if ( mysql_num_rows( $qry ) > 0 ) {
session_regenerate_id();
while ( $data = mysql_fetch_array( $qry ) ) {
$_SESSION['user_id'] = $data['user_id'];
$_SESSION['user_group'] = $data['user_group'];
$_SESSION['user'] = $data['user'];
$_SESSION['user_name'] = $data['user_name'];
// Debug
echo $_SESSION['user_id'];
echo $_SESSION['user_group'];
echo $_SESSION['user'];
echo $_SESSION['user_name'];
}
}
}
login( "username", "password" );
?>
I've fixed your code for you.