Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/242626-get-session-user_id-and-user_name/
Share on other sites

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

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

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.

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 . "')");

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.