user2921081 user2921081 - 3 months ago 16
MySQL Question

Trying to connect two databases to a php file and store user session

Ok, so what I'm trying to basically is connect two databases to my index.php file and then use one of the databases to log the user in and one of the other databases to store the session for the user.

What I cant figure out how to do is store the session of a user. What I want to do is have the users information to be shown such as email and username things like that and then when they logout I want that information to disappear with the session, but currently when a user logs-in the information stays the say from the old user and it doesnt change.

I've included my code below. In my code when I mention session like its a database thats because it is I created a database and table called session and my other database is called database and the table is called admin.

Index

<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);

$sql = "SELECT id FROM admin WHERE username = '$myusername' and passcode = '$mypassword'";
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
$session_id = session_id();
$sql = "INSERT INTO session(session_id,username) VALUES('$session_id','$myusername')";
mysqli_query($db,$sql);
header("Location: test2.php");
}else {
$error = "";
}
}
?>


Config

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'vikings1');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>


Session

<?php
include('config.php');
session_start();

$user_check = $_SESSION['myusername'];

$ses_sql = mysqli_query($db,"select username from admin where username = '$user_check' ");

$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_session = $row['username'];

?>


Test 2 Page (This is the page I want to login to so it displays user info.)

<?php
session_start();
include("config.php");

$servername = "localhost";
$username = "root";
$password = "*****";
$dbname = "database";
$dbname2 = "session";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname, $dbname2);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$session_id = session_id();

$sql = "SELECT id, username, passcode, email, Address, City, Country, Zip, FirstName, LastName, About FROM admin WHERE username = (SELECT username FROM session WHERE session_id = '$session_id')";

$result = $conn->query($sql);
$row = $result->fetch_assoc();

?>

Answer

As I said in comments:

"Basically, what you need to do is assign a session array to a post array and then pass that session to other pages (and query in a WHERE clause), then destroy the session on logout."

So this is the logic you can use and as a basic example:

Sidenote:

It would most likely take me more time to go over your entire code to fix it. This was easier/faster.

<?php 

session_start();

$_POST['user'] = "John"; // Take the POST array from your named input

if (!isset($_SESSION['var'])) {

  $_SESSION['var'] = $_POST['user'];
} else {
  echo "It seems the session is already set " . $_SESSION['var'];
}
$username = $_SESSION['var'];

echo $username;

...then check if the session is set in subsequent pages and apply the same logic.

Also heed the comments left under your question.

Reference to destroy the session:

Comments