esoteric Posted July 22, 2011 Share Posted July 22, 2011 Is there anyway to get the users current session variables, like there user_id and user_name and save them to another database? what im trying to do is save the details of an order form to a database upon hitting submit, so far i have it sending over a few details from the order form but i want it to get the users id and username and save that on the database too (users have to be logged in already to actually send an order). So far my attempts have failed, any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/ Share on other sites More sharing options...
gristoi Posted July 22, 2011 Share Posted July 22, 2011 you need to know what the session variables are called. then it is just a matter of doing : <?php $user_id = $_SESSION['userId']; // given that the session is called userId $username = $_SESSION['username']; // given that the session is called username ?> then use these variables to insert into the db Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246100 Share on other sites More sharing options...
ScotDiddle Posted July 22, 2011 Share Posted July 22, 2011 esoteric, What is failing ? Did you try something like this: On the logon page, : $_SESSION['who'] = $_POST['who']; $_SESSION['ID'] = $_POST['ID']; Then, later, to store values in a DB: $who = $_SESSION['who']; $ID = $_SESSION['ID']; ScotL. Diddle, Richmond VA Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246101 Share on other sites More sharing options...
esoteric Posted July 22, 2011 Author Share Posted July 22, 2011 On my login page i have $_SESSION['user_id']= $id; $_SESSION['user_name'] = $full_name; I am writing the values to the database with mysql_connect("xxx.xxx.com", "$username", "$password") or die( "Unable to connect to database"); mysql_select_db("xxx") or die( "Unable to select database"); mysql_query("INSERT INTO orderTable (ip, userId, userName, id, orderNum, orderDate, custEmail, firstName, lastName) VALUES ('" . $REMOTE_ADDR . "', '" . $userId = $_SESSION['user_id'] . "', '" . $userName = $_SESSION['user_name'] . "','', " . $order . ", '" . $today . "', '" . $b_email . "', '" . $b_first . "', '" . $b_last . "')"); The table show all the details apart from the userid and username, they just appear blank. I really have no idea whats going on. Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246119 Share on other sites More sharing options...
gristoi Posted July 22, 2011 Share Posted July 22, 2011 mysql_connect("xxx.xxx.com", "$username", "$password") or die( "Unable to connect to database"); mysql_select_db("xxx") or die( "Unable to select database"); mysql_query("INSERT INTO orderTable (ip, userId, userName, id, orderNum, orderDate, custEmail, firstName, lastName) VALUES ('" . $REMOTE_ADDR . "', '" . $_SESSION['user_id'] . "', '" .$_SESSION['user_name'] . "','', " . $order . ", '" . $today . "', '" . $b_email . "', '" . $b_first . "', '" . $b_last . "')"); Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246124 Share on other sites More sharing options...
esoteric Posted July 22, 2011 Author Share Posted July 22, 2011 Thanks for the reply. I tried that and its still isn't working. I tried adding; $userId = $_SESSION['user_id'] or die("Cannot find UserID, make sure you are logged in."); $userName = $_SESSION['user_name'] or die("Cannot find UserName, make sure you are logged in."); at the top of my script and calling the $userId and $userName instead but its return the error string i put in when i hit submit "Cannot find UserID, make sure you are logged in." Any ideas what i may be doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246128 Share on other sites More sharing options...
gristoi Posted July 22, 2011 Share Posted July 22, 2011 do you have session_start at the top of each page? Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246134 Share on other sites More sharing options...
AyKay47 Posted July 22, 2011 Share Posted July 22, 2011 try a print_r($_SESSION); to confirm that these sessions are being stored with the proper values for later use Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246136 Share on other sites More sharing options...
esoteric Posted July 22, 2011 Author Share Posted July 22, 2011 @gristoi umm... Well i do now! You Sir are my hero, i really do owe you beer! Thank you very much, that got it. @AyKay47 Thank you, what does print_r do? Just print it on screen? Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246137 Share on other sites More sharing options...
AyKay47 Posted July 22, 2011 Share Posted July 22, 2011 seems like you forgot to include session_start() at the top of the page, but to answer your question directed towards me, a print($_SESSION); will print all of the keys and values that are stored in the session array at the time of calling the print_r Quote Link to comment https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/#findComment-1246167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.