webguync Posted April 15, 2010 Share Posted April 15, 2010 I need to echo out what values are being stores in a Session array the function which stores the data is: function session_store($data) { $_SESSION = array_merge($_SESSION, $data); } how would I do this? Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/ Share on other sites More sharing options...
Pikachu2000 Posted April 15, 2010 Share Posted April 15, 2010 If all you need to do is see them: print_r($_SESSION); Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042686 Share on other sites More sharing options...
webguync Posted April 15, 2010 Author Share Posted April 15, 2010 this right, I didn't see anything echo function session_store($data) { $_SESSION = array_merge($_SESSION, $data); print_r($_SESSION); } can I get the info from the session emailed? Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042690 Share on other sites More sharing options...
teamatomic Posted April 15, 2010 Share Posted April 15, 2010 Did you use session_start? Also, $_SESSION is by default an array, if you assign a value to $_SESSION($_SESSION='this', it is no longer an array but a variable and thus can only hold one value. Best would be to do: $_SESSION['my_array'] then you can use session multiple times. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042695 Share on other sites More sharing options...
webguync Posted April 15, 2010 Author Share Posted April 15, 2010 yes, I did. Here is all of my code. <?php //ini_set("display_errors","1"); //ERROR_REPORTING(E_ALL); function my_error_handler($errno, $errstr, $errfile, $errline, array $errcontext = array()) { die($errstr); } set_error_handler('my_error_handler'); session_start(); $con = mysql_connect("localhost", "uname", "pw") or trigger_error('Could not connect: ' . mysql_error()); mysql_select_db("DBName", $con) or trigger_error(mysql_error()); class EmptyCredentialsException extends Exception {} class InvalidCredentialsException extends Exception {} // Same checking stuff all over again. function clean($value, $db = null) { $value = strip_tags($value); $value = htmlentities($value); if(function_exists('mysql_real_escape_string') && mysql_real_escape_string($value, $db) !== FALSE) return mysql_real_escape_string($value, $db); else return addslashes($value); } function login($username, $password, $db = null) { if (empty($username) || empty($password)) { throw new EmptyCredentialsException(); } $username = clean($username, $db); $pwid = clean($password, $db); $pwid = intval($pwid); $query = "SELECT name, username,user_id FROM roster_March2010 WHERE pwid = MD5('$pwid') AND username = '$username'"; $result = mysql_query($query, $db); if ($result && mysql_num_rows($result)) { $user = mysql_fetch_assoc($result); user_update(array('login_timestamp' => time()), $username, $db); session_regenerate_id(); $meta_data = array('ip' => $_SERVER['REMOTE_ADDR'], 'browser' => $_SEVER['HTTP_USER_AGENT']); session_store($user + $meta_data); return true; } throw new InvalidCredentialsException(); } function user_update($data, $username, $db = null) { $query = 'UPDATE roster_March2010 SET '; $data = array_map('user_update_callback', $data, array_keys($data)); $query = $query . implode(', ', $data); $query = "$query WHERE username = '$username'"; $result = mysql_query($query, $db) or trigger_error(mysql_error()); return $result && mysql_affected_rows($result); } function user_update_callback($value, $key) { return "$key = '{clean($value)}'"; } function session_is_auth() { return (isset($_SESSION['ip']) && isset($_SESSION['browser'])) && (($_SESSION['ip'] === $_SERVER['REMOTE_ADDR']) && ($_SESSION['browser'] === $_SERVER['HTTP_USER_AGENT'])); } function session_store($data) { $_SESSION = array_merge($_SESSION, $data); print_r($_SESSION); } if (isset($_POST['submit'])) { try { login($_POST['username'], $_POST['pwid'],$con); } catch (EmptyCredentialsException $e) { echo "<h2 class='fail'>Please fill in both your username and password to access your exam results.<br />", "<br >You will be redirected back to the login screen in five seconds.</h2>"; echo "<meta http-equiv='refresh' content='5; url=StudentLogin.php'>"; exit; } catch (InvalidCredentialsException $e) { echo "<h2 class='fail'>You have entered a username or password that does not match our database records.", " please try again.<br><br>You will be redirected back to the login screen in five seconds.</h2> "; echo "<meta http-equiv='refresh' content='5; url=StudentLogin.php'>"; exit(); } } // Start a session. If not logged in will be redirected back to login screen. if (!session_is_auth()) { header("Location:StudentLogin.php"); exit; } echo "<table id='header'><tr><td align='middle'><div id='welcome'><h3>Welcome! You are now logged in " . $_SESSION['name'] . "</h3></td></tr>"; echo "<tr><td><a class='logout' href='LogoutStudent.php'>Logout</a></td></tr></table>"; ?> so this $_SESSION = array_merge($_SESSION, $data); should be: $_SESSION[$data] ? Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042757 Share on other sites More sharing options...
teamatomic Posted April 16, 2010 Share Posted April 16, 2010 session_start should go directly after the opening php tag. session is an array, so unless $data has a value you want to be the key you should use$_SESSION['data'] HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042768 Share on other sites More sharing options...
webguync Posted April 16, 2010 Author Share Posted April 16, 2010 I am still not getting the array values to display. Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042774 Share on other sites More sharing options...
l0ve2hat3 Posted April 16, 2010 Share Posted April 16, 2010 try this: $data=array_merge($user,$meta_data); session_store($data); <?php //ini_set("display_errors","1"); //ERROR_REPORTING(E_ALL); function my_error_handler($errno, $errstr, $errfile, $errline, array $errcontext = array()) { die($errstr); } set_error_handler('my_error_handler'); session_start(); $con = mysql_connect("localhost", "uname", "pw") or trigger_error('Could not connect: ' . mysql_error()); mysql_select_db("DBName", $con) or trigger_error(mysql_error()); class EmptyCredentialsException extends Exception {} class InvalidCredentialsException extends Exception {} // Same checking stuff all over again. function clean($value, $db = null) { $value = strip_tags($value); $value = htmlentities($value); if(function_exists('mysql_real_escape_string') && mysql_real_escape_string($value, $db) !== FALSE) return mysql_real_escape_string($value, $db); else return addslashes($value); } function login($username, $password, $db = null) { if (empty($username) || empty($password)) { throw new EmptyCredentialsException(); } $username = clean($username, $db); $pwid = clean($password, $db); $pwid = intval($pwid); $query = "SELECT name, username,user_id FROM roster_March2010 WHERE pwid = MD5('$pwid') AND username = '$username'"; $result = mysql_query($query, $db); if ($result && mysql_num_rows($result)) { $user = mysql_fetch_assoc($result); user_update(array('login_timestamp' => time()), $username, $db); session_regenerate_id(); $meta_data = array('ip' => $_SERVER['REMOTE_ADDR'], 'browser' => $_SEVER['HTTP_USER_AGENT']); $data=array_merge($user,$meta_data); session_store($data); return true; } throw new InvalidCredentialsException(); } function user_update($data, $username, $db = null) { $query = 'UPDATE roster_March2010 SET '; $data = array_map('user_update_callback', $data, array_keys($data)); $query = $query . implode(', ', $data); $query = "$query WHERE username = '$username'"; $result = mysql_query($query, $db) or trigger_error(mysql_error()); return $result && mysql_affected_rows($result); } function user_update_callback($value, $key) { return "$key = '{clean($value)}'"; } function session_is_auth() { return (isset($_SESSION['ip']) && isset($_SESSION['browser'])) && (($_SESSION['ip'] === $_SERVER['REMOTE_ADDR']) && ($_SESSION['browser'] === $_SERVER['HTTP_USER_AGENT'])); } function session_store($data) { $_SESSION = array_merge($_SESSION, $data); print_r($_SESSION); } if (isset($_POST['submit'])) { try { login($_POST['username'], $_POST['pwid'],$con); } catch (EmptyCredentialsException $e) { echo "<h2 class='fail'>Please fill in both your username and password to access your exam results.<br />", "<br >You will be redirected back to the login screen in five seconds.</h2>"; echo "<meta http-equiv='refresh' content='5; url=StudentLogin.php'>"; exit; } catch (InvalidCredentialsException $e) { echo "<h2 class='fail'>You have entered a username or password that does not match our database records.", " please try again.<br><br>You will be redirected back to the login screen in five seconds.</h2> "; echo "<meta http-equiv='refresh' content='5; url=StudentLogin.php'>"; exit(); } } // Start a session. If not logged in will be redirected back to login screen. if (!session_is_auth()) { header("Location:StudentLogin.php"); exit; } echo "<table id='header'><tr><td align='middle'><div id='welcome'><h3>Welcome! You are now logged in " . $_SESSION['name'] . "</h3></td></tr>"; echo "<tr><td><a class='logout' href='LogoutStudent.php'>Logout</a></td></tr></table>"; ?> Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042775 Share on other sites More sharing options...
webguync Posted April 16, 2010 Author Share Posted April 16, 2010 l0ve2hat3 was the two lines you posted in the first part the only change? Shouldn't I still do print_r($_SESSION); trying to get a text result of what is being stored in the session array. Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042779 Share on other sites More sharing options...
l0ve2hat3 Posted April 16, 2010 Share Posted April 16, 2010 @webguync Yes, I modified your login function on line 45-46. Everything else is the same. I put it all in the second code from that post. Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042782 Share on other sites More sharing options...
teamatomic Posted April 16, 2010 Share Posted April 16, 2010 I am lost with what the heck you are doing to that poor timestamp. You have one element in the array, what are you trying to with array_map? In the callback try this: $result= clean($value); return $result; Then you need to do $_SESSION['login_timestamp']=$data[0]; You are getting overly complicated so needlessly. array_map is to run through an array throwing elements at your own function to do things that php built in functions or simple statements cant accomplish. With all you do a simple $timestamp=clean($timestamp); would accomplish the same thing. But why are you even cleaning it? Your script creates it, it is not user input and needs no cleaning, besides the fact that the clean function has no effect on a timestamp. You have to rethink what you are doing and how to best accomplish the task. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1042789 Share on other sites More sharing options...
webguync Posted April 17, 2010 Author Share Posted April 17, 2010 @teamatonic are you referring to here? function user_update_callback($value, $key) { return "$key = '{clean($value)}'"; } change to this? function user_update_callback($value, $key) { $result= clean($value); return $result; } Link to comment https://forums.phpfreaks.com/topic/198695-echo-out-session-info/#findComment-1043712 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.