Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post

    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.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,102
    Thanks
    23
    Thanked 594 Times in 593 Posts
    It should be a lot simpler to use sessions http://www.w3schools.com/php/php_sessions.asp

  • Users who have thanked sunfighter for this post:

    Paul Williams (01-20-2013)

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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
    Last edited by BubikolRamios; 01-20-2013 at 05:21 PM.
    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

  • Users who have thanked BubikolRamios for this post:

    Paul Williams (01-20-2013)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Paul Williams (01-20-2013)

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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

  • #6
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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.

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

    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

  • Users who have thanked BubikolRamios for this post:

    Paul Williams (01-21-2013)

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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

  • #8
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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

  • #9
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Browser does not 'like it' coz there is no script tag , I presume:
    <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

  • Users who have thanked BubikolRamios for this post:

    Paul Williams (01-21-2013)

  • #10
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    nope

    script tags are in:

    <script type="text/javascript">

    ....


    </script>

  • #11
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    wrap this:
    <?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

  • Users who have thanked BubikolRamios for this post:

    Paul Williams (01-21-2013)

  • #12
    New Coder
    Join Date
    Dec 2011
    Posts
    59
    Thanks
    51
    Thanked 1 Time in 1 Post
    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.

  • #13
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    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

  • Users who have thanked BubikolRamios for this post:

    Paul Williams (01-21-2013)

  • #14
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,102
    Thanks
    23
    Thanked 594 Times in 593 Posts
    $_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.

  • Users who have thanked sunfighter for this post:

    Paul Williams (01-21-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •