cooldude832 Posted July 22, 2007 Share Posted July 22, 2007 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? Link to comment https://forums.phpfreaks.com/topic/61180-solved-issue-registering-sessions-via-a-loop/ Share on other sites More sharing options...
trq Posted July 22, 2007 Share Posted July 22, 2007 For starters, you have an error in your query. Should be... $q = "SELECT $sqlfields FROM `Users` WHERE `Username`='$username' && `Password`='$password' LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/61180-solved-issue-registering-sessions-via-a-loop/#findComment-304710 Share on other sites More sharing options...
GingerRobot Posted July 22, 2007 Share Posted July 22, 2007 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 Link to comment https://forums.phpfreaks.com/topic/61180-solved-issue-registering-sessions-via-a-loop/#findComment-304713 Share on other sites More sharing options...
cooldude832 Posted July 22, 2007 Author Share Posted July 22, 2007 no the foreach loop uses $fields as $value which are the same as the fields in the it worked with taking the quotes of '$sqlfield' i thought you needed to quote every variable like that? oh well its working Link to comment https://forums.phpfreaks.com/topic/61180-solved-issue-registering-sessions-via-a-loop/#findComment-304714 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.