Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 04:58 PM   PM User | #1
Paul Williams
New Coder

 
Join Date: Dec 2011
Posts: 59
Thanks: 51
Thanked 1 Time in 1 Post
Paul Williams is an unknown quantity at this point
store username in database ??

after a user logs in, i can pull up their username and print it out on the webpage with <?php $_SESSION['MM_Username'] ?>.

however, i want to take that text value and store it in the database along side a timestamp. so that i know when someone is logged in and what they were doing.

simple as this sounds, i'm finding it not so simple.

how do you take the username of the person logged into your website and store that in the MySQL database?

anyone know how?

thanks,
Paul W.
Paul Williams is offline   Reply With Quote
Old 01-20-2013, 05:01 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
It should be a lot simpler to use sessions http://www.w3schools.com/php/php_sessions.asp
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
Paul Williams (01-20-2013)
Old 01-20-2013, 05:16 PM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
All, people here knows how, but there are a zilion of ways.
Get sme basics, start reading here: http://www.w3schools.com/sql/sql_intro.asp
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 01-20-2013 at 05:21 PM..
BubikolRamios is offline   Reply With Quote
Users who have thanked BubikolRamios for this post:
Paul Williams (01-20-2013)
Old 01-20-2013, 05:34 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The behaviour you describe requires either using completely database controlled sessions, or to use the session_set_save_handler for simplicity. That lets you override the functionality of sessions so you can transparently use and modify the session without needing to implement any new control code to the existing scripts. If you set the save handlers, you can use a database or still use the filesystem and use partial database control for things like a timestamp.
This is only relevant if you want to aggregate the session data. So to see how many people are online for example. If you just want to know the last time there was activity for a user, you can store that in the session itself (only that user will have access to it though).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Paul Williams (01-20-2013)
Old 01-20-2013, 06:27 PM   PM User | #5
Paul Williams
New Coder

 
Join Date: Dec 2011
Posts: 59
Thanks: 51
Thanked 1 Time in 1 Post
Paul Williams is an unknown quantity at this point
ok, i read everything you guys referenced and i'm still lost

using the $_SESSION variable is fine, i understand that.
but when i try to assign that "text" value to a different variable, like an object literal or something, and then "post" that info to the mysql database, it all just does not want to happen. nothing crashes, its just that the value of the username never makes it to the database.

i was reading about the session set save handler, sounds easy,... oh boy, looking at the code, totally lost.

this should be easy, so i would assume.
still stuck

Paul
Paul Williams is offline   Reply With Quote
Old 01-20-2013, 11:16 PM   PM User | #6
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
some clarifications:

Code:
$_SESSION['MM_Username']
You obviously have that, so showing that to user & reading that from form again is pointless, as the second you have it, means user logged in and you can do update of db, to log last visit.

Quote:
how do you take the username of the person logged into your website
Read above.

Quote:
and store that in the MySQL database
If you don't have any SQL to show us, you are not using it (so the stuff cant get to db), study my link given above again. If you do, and nothing happens in db, there must be some error.

Show us Insert or update SQL if you have it.

Mybe you inherited some project with object oriented db stuff (like hyberate,...), which I think you did not, but in case ...then only god can help you (-:

Hope this will turn some light on.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Users who have thanked BubikolRamios for this post:
Paul Williams (01-21-2013)
Old 01-21-2013, 01:29 AM   PM User | #7
Paul Williams
New Coder

 
Join Date: Dec 2011
Posts: 59
Thanks: 51
Thanked 1 Time in 1 Post
Paul Williams is an unknown quantity at this point
here's my code on the main page:

Code:
<head>
<?php     if (!isset($_SESSION)) {session_start();     ?>
</head>

<body>
<p>    <?php  echo $_SESSION['MM_Username']    ?>    </p>
</body>
works fine, splashes out the username on the screen, so i know i have it and can splash it out as text


so i try and pass the value of the session variable to an object literal and use jqueary's $.post() to store that value in the database.
Code:
<head>
$(document).ready(function( ) {

var dataOperator = {  dataOper :  <?php $_SESSION['MM_Username'] ?>};
$.post('putOperator-server-script.php', dataOperator , function(dataIn,status){ });
the browser doesn't like this, that is it doesn't like the way i'm trying to assign the value of the username session variable to an object literal.


then i pass the $.Post() data to the server by way of the url 'putOperator-server-script.php'; that SQL code is the following:

Code:
<?php require_once('Connections/connDB.php'); ?>
<?php
  $data = $_POST['dataOper'];
  mysql_select_db($database_connDB, $connDB);
  $sql = "INSERT INTO cpqc_status (operator) VALUES ($data)";
  mysql_query( $sql ) or die( mysql_error() );
  echo $data;
?>
that's my code, i think i'm messing up on trying to assign the value of the session variable to the object literal (?) (i guess?)

is there something grossly wrong here??

thanks, i'll go back and re-read your link, more than once
Paul
Paul Williams is offline   Reply With Quote
Old 01-21-2013, 01:37 AM   PM User | #8
Paul Williams
New Coder

 
Join Date: Dec 2011
Posts: 59
Thanks: 51
Thanked 1 Time in 1 Post
Paul Williams is an unknown quantity at this point
let me clarify something

i was splashing out the username on the screen just to test that i can access that value as a text string accurately

i need to store the username in the db, because my user will select changes to graphics on the home page, then a different user comes along and does the same thing, then another, and so on. each time a user is logged on and changes the graphic to one of the preset options, i log who made the change. only one user can be logged on at any one time. so i'm simply tracking who the users are and when they are on the sight and what changes they made to the preset graphic options.

i may need to splash out the username on the main page later, but for now it's just going in the database for my log.

thanks,
Paul
Paul Williams is offline   Reply With Quote
Old 01-21-2013, 02:02 AM   PM User | #9
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Browser does not 'like it' coz there is no script tag , I presume:
Quote:
<head>
<script>
$(document).ready(function( ) {
...
</script>
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Users who have thanked BubikolRamios for this post:
Paul Williams (01-21-2013)
Old 01-21-2013, 02:09 AM   PM User | #10
Paul Williams
New Coder

 
Join Date: Dec 2011
Posts: 59
Thanks: 51
Thanked 1 Time in 1 Post
Paul Williams is an unknown quantity at this point
nope

script tags are in:

<script type="text/javascript">

....


</script>
Paul Williams is offline   Reply With Quote
Old 01-21-2013, 03:11 AM   PM User | #11
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
wrap this:
Quote:
<?php require_once('Connections/connDB.php'); ?>
<?php
$data = $_POST['dataOper'];
mysql_select_db($database_connDB, $connDB);
$sql = "INSERT INTO cpqc_status (operator) VALUES ($data)";
mysql_query( $sql ) or die( mysql_error() );
echo $data;
?>
into try ... except

there are some samples how to do that:
http://stackoverflow.com/questions/2...with-phpmailer
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Users who have thanked BubikolRamios for this post:
Paul Williams (01-21-2013)
Old 01-21-2013, 04:21 AM   PM User | #12
Paul Williams
New Coder

 
Join Date: Dec 2011
Posts: 59
Thanks: 51
Thanked 1 Time in 1 Post
Paul Williams is an unknown quantity at this point
i went to the link you showed and looked it over hard
never seen that code before
not sure what they are doing, haven't a clue how to relate that to mine
sorry

doing some research elsewhere, some are saying that $_SESSION variables are on the server side, while Javascript variables are on the client side and that you cannot mix the two

one said to just declare:
var a = <?php echo $_SESSION['MM_Username']; ?>

but that doesn't work either.
Paul Williams is offline   Reply With Quote
Old 01-21-2013, 10:43 AM   PM User | #13
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
I assume that should do what it looks like it should do. Doh I don't know PHP
Quote:
Originally Posted by Paul Williams View Post
var a = <?php echo $_SESSION['MM_Username']; ?>

but that doesn't work either.
What do you mean it does not work ?

Try

Code:
...
<script>
alert(a);
</script>
</html>
That should pop up your name that you transfered from server to client (into javascript part of client).

And prove that it works.


I would formulate the saying prom previous post that some suggested it to you a bit different: When server spits whatever it spits to client, then it (the server) would not know what is on client even if you kill it (the server) (-:

From my previous post: Google for exception handling and you will find text like (no matter which language):
try...except ... finaly

Read that.

It means, whatewer seems did nothing, it probably means that you can get what went wrong after except part of code.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search
BubikolRamios is offline   Reply With Quote
Users who have thanked BubikolRamios for this post:
Paul Williams (01-21-2013)
Old 01-21-2013, 05:17 PM   PM User | #14
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
$_SESSION['MM_Username'] is something that comes from using dreamweaver. I would not trust it nor use it. When a user logs into a site they are normally asked for their user name and their password. So you should have their user name as a variable already. I was thinking that you would set a session variable to that along with the log in time and not use $_SESSION['MM_Username']. I then thought that you could use sessions to keep track of "what they were doing" on your site and when they logged out or the session time out you would then store that information.

I do believe on a site you can use cPanel and get over all statistics about your site, but not what individuals do.

Lets see how you would "i want to take that text value and store it in the database along side a timestamp." from post one.

User goes to your site and get a login screen. He/she logs in giving you his user name and password in a form that is passed to a php program that cleans the variable and looks into the database to see if he is registered. At the point if he is registered you can make a DB entree with his name and time. Simple. Also at this time you can set the session variable for her name so you can follow her on your site.

When things are over you can get the variable
Code:
<?php
    session_start();

   foreach ($_SESSION as $key=>$val)
    echo $key." ".$val."<br/>";
?>
Or instead of echoing them put them into an array and then put the array into the db.
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
Paul Williams (01-21-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:36 AM.


Advertisement
Log in to turn off these ads.