How to insert into a table after a successful session login

insert into session
informatica repository query to get session statistics
how to store session variable in database
insert _session username into database
sessions table
store session in database c#
how to insert data using session in php
store session in database java

I'm making a login script with a simple manage session feature like Facebook

As a start I have created a table called "users_sessions"

This is the code :

$database->query("
    CREATE TABLE `users_sessions` (
        `session_id` int(10) UNSIGNED NOT NULL,
        `session_date` datetime NOT NULL,
        `user_id` int(10) UNSIGNED NOT NULL,
        `user_browser` varchar(64) COLLATE utf8mb4_bin NOT NULL,
        `user_os` varchar(64) COLLATE utf8mb4_bin NOT NULL,
        `user_ip` varchar(64) COLLATE utf8mb4_bin NOT NULL,
         PRIMARY KEY (`session_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");

So what I want to do is when users fill in the box the right username and password automatically the script will insert into this table the right information of the successful user log.

and this is the index.php :

 $_POST['password'] = User::hash_password($_POST['password']);
if(!empty($_POST) && empty($_SESSION['error'])) {
    /* If remember me is checked, log the user with cookies for 30 days else, remember just with a session */
    if(isset($_POST['rememberme'])) {
        setcookie('username', $_POST['username'], time()+60*60*24*30);
        setcookie('password', $_POST['password'], time()+60*60*24*30);
        setcookie('user_id', User::login($_POST['username'], $_POST['password']), time()+60*60*24*30);


    } else {
        $_SESSION['user_id'] = User::login($_POST['username'], $_POST['password']);
    }
    redirect();
}

And this is my database config :

<?php

/* CONNECTION */
$database_connection = new StdClass();
$database_connection->server = 'localhost';
$database_connection->username = 'root';
$database_connection->password = '';
$database_connection->name = 'wearesocio';
$database = new mysqli($database_connection->server, $database_connection->username, $database_connection->password, $database_connection->name);
if($database->connect_error) {

    header('Location: '.$_SERVER['PHP_URL']."install");
}

/* DB CLASS */
Database::$database = $database;
/* DEBUGGING */
define('DEBUGGING', true);

?>
if(isset($_POST['rememberme'])) {
    setcookie('username', $_POST['username'], time()+60*60*24*30);
    setcookie('password', $_POST['password'], time()+60*60*24*30);
    setcookie('user_id', User::login($_POST['username'], $_POST['password']), time()+60*60*24*30);
    insert_into_db($params);
} else {
    $_SESSION['user_id'] = User::login($_POST['username'], $_POST['password']);
    insert_into_db($params);
}

function insert_into_db($params){
    // write insert query
}

In an ideal world, I'd use PDO with bound parameters in the insert function, have every table with PRIMARY_KEYs set, and user inputs sanitized. And of course, hash passwords before storage as per How to use password_hash

UPDATE

if(!empty($_POST) && empty($_SESSION['error'])) {
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $password_raw = $_POST['password'];

    // Get these data
    $session_params = array();
    $session_id = 1;
    $session_date = time();
    $user_id = User::login($username, $password_raw);
    $user_browser = '';
    $user_os = '';
    $user_ip = '';
    array_push($session_params, $session_id, $session_date, $user_id, $user_browser, $user_os, $user_ip);

    if(isset($_POST['rememberme'])) {
        setcookie('username', $username, time()+60*60*24*30);
        //setcookie('password', $_POST['password'], time()+60*60*24*30);
        setcookie('user_id', $user_id, time()+60*60*24*30);
        insert_session_into_db($session_params);
    } else {
        $_SESSION['user_id'] = $user_id;
        insert_session_into_db($session_params);
    }
    redirect();
}

function insert_session_into_db($session_params)
{
    // include database config file, or instantiate your Database class here

    extract($session_params);

    $sql = "INSERT INTO users_sessions (session_id, session_date, user_id, user_browser, user_os, user_ip)
            VALUES ('".$session_id."', '".$session_date."', '".$user_id."', '".user_os."', '".$user_ip."')";
    $res = $database->query($sql);
    if ($res === true) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $database->error;
    }
    $database->close();
}

How to insert into a table after a successful session login, Deletion of Records from Tables As software is used, we keep adding new records and we enter “Blank”; and in the field “Value after modification,” we enter “NEW. The date of the session 3. The time at which the login was successful 4. Use the post session email you can get affected rows, rejected rows and target affected rows, rejected rows to be inserted into a table after session run completes. 2. use omcmd command to get the session statistics 3. Grep the session log file for the required information

this is my solution

I have created some functions for getting user browser or user ip etc Then creating this function below to add it after the $_SESSION['user_id']

    function insert_into_users_sessions($user_id){
    // here add your database class name
    global $database;

    $user = $_SESSION['user_id'];
    $date = new DateTime();
    $date = $date->format('Y-m-d H:i:s');
    $user_browser = get_user_browser();
    $user_ip = get_user_ip();
    $user_os = get_user_os();

    @$database->query("INSERT INTO `user_sessions` (`user_id`, `session_ip`, `session_os`, `session_browser`, `session_date`) VALUES ('$user', '$user_ip', '$user_os', '$user_browser', '$date')");

}

Session Log information to be loaded into a table after session run , This piece of code will set the :APP_USER item and redirect to the value specified by the P_FLOW_PAGE parameter after a successful login. To prevent the username from being uppercased before it is passed to your Suppose you insert an extra 9,999 rows into the user_repository table from a SQL*Plus session​:  I finished login with session pages and include session onto every page. So when the user login it check and display from "user" table session: Hello "user" The problem is with "insert into" statement, when john or david logs in and then they post something into database.

for example , you can add it at the end of login() method

public static login(){

  // you put the login logic here 


     // when the user login info are correct
     if(login == true ){

      // add the user to users_sessions

     }
}

Software Design: A Comprehensive Guide to Software Development , UPBY field is auto-insert field with $_SESSION["UserID"]. new VAR data then the UPBY field in the table should be auto-inserted with $_SESSION[“UserID”] value. Action so far done: 1. After Successful login event on Login Page You don't have to load session userid in after succesully login event. I want to store the username in a session variable immediately after each successful login with this statement: Session["Username"]=User.Identity.Name; If I put this statement in the page

Pro Oracle Application Express 4, sess_storage_init.sql drop table if exists sessions cascade; create table sessions​( id serial, if ($num === 0) { $sql = "insert into sessions (sess_id, _access, data) values ". 'badpass') { $loggedIn = TRUE; $displayBlock = "Logged in successfully"; So, if the user forgets to logout, the session will be killed after 24 minutes. I am testing inserts into a heap from 2 sessions, I am inserting 10million rows from 1 session which takes 44 seconds but when I insert from 2 sessions the first insert takes about 44 seconds and the second insert only starts after the first one finishes since it's waiting on intent exclusive lock (LCK_M_IX).

[SOLVED] How to insert $_SESSION values into the auto insert field , After a successful login, a Session for that remote client is started. registerLoginSession() inserts a new row into that table, with the current Session ID (given  After performing successful and failed login processes, the events will be collected and displayed by the SQL Server Extended Events session. For example, the successful login process properties, including the user name, the host name, the application used for the login and other useful information will be displayed as shown clearly below:

Storing session data in a database on the server, Next, you should insert a row in the t1 table to demonstrate that placing a You should receive a message indicating that the statement executed successfully, lock, any sessions that try to access the t1 table can only read from that table, but After you locked the table with a WRITE lock, you tested your ability to write to  First as soon as user login to system (means when you are validating user data and generating session-token), you need to save the user-id in the session, then when ever he performs any insert operation you can get the user information based on session-token and insert it.

Comments
  • Don't store the password in a cookie! You can use the existence of a valid session as proof of authentication.
  • @AlexHowansky don't worry Iwill delete the remember me, so where should i add the insert form and what is the right insert form ?
  • See this and this.
  • Also note that an INT type column is not sufficient for a session id field. You'll want a string. (Not sure what PHP's default session id length is, but let's be safe and say at least 64 chars.)
  • @MohcineMimoudi Please edit your question to include a minimal reproducible example of your problem, which can be tested by others, which shows how you try to create a new row in the database/table. Currently you are not showing the code which sends the INSERT INTO SQL query.