Jump to content

[SOLVED] Issue Registering sessions via a loop


cooldude832

Recommended Posts

I'm trying out a new method of logging in and it looks like this:

<?php
function login(){
session_unset();
connectSQL();
$username = mysql_escape_string(trim($_POST['username']));
$password = md5(trim($_POST['password']));
// Create query
$fields = array("UserID","Username");
$sqlfields = implode(",",$fields);
$q = "SELECT '$sqlfields' FROM `Users` WHERE `Username`='$username' && `Password`='$password' LIMIT 1";
// Run query
$r = mysql_query($q) or die(mysql_error());
if(mysql_num_rows($r)){
	$row = mysql_fetch_array($r);
	foreach($fields as $value){
		$_SESSION[$value] = $row[$value];
	}
	// Redirect to member page
	$_SESSION['logged_in'] = "yes";
	header("Location: play.php");
}	
else{
  // Login not successful
die("Sorry, could not log you in. Wrong login information.<br/>");
include("login.html");
}
}//End of login
?>

It is working, to login, but when i try to retrive my registered sessions i get:

 

UserID :

Username :

logged_in : yes

off the code

<?php
foreach($_SESSION as $key => $value){
echo $key." : ".$value."<br />";
}
?>

any ideas?

 

Edit:

I've tried putting the $_SESSION[$value] = as $row["$value"] and as $row['$value'] with no luck

I've tried more sessions and they can't even register any ideas?

Also, shouldn't this line:

 

$_SESSION[$value] = $row[$value];

 

Be:

 

$_SESSION[$fields] = $row[$value];

 

Since it is the field names that i presume you want to use as the keys for the array? e.g. you want userID to be the key, not its value

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.