尝试将两个数据库连接到php文件并存储用户会话 [英] Trying to connect two databases to a php file and store user session

查看:88
本文介绍了尝试将两个数据库连接到php文件并存储用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我基本上想要将两个数据库连接到我的index.php文件,然后使用其中一个数据库登录用户,并使用另一个数据库存储该用户的会话。

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.

我在下面添加了我的代码。在我的代码中,当我提到会话之类的数据库时,这是因为它创建了一个称为会话的数据库和表,而我的另一个数据库称为数据库,该表称为admin。

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', '******');
   define('DB_DATABASE', 'database');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

会话

<?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'];

 ?>

测试2页面(这是我要登录的页面,因此它显示用户信息。)

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();

?> 


推荐答案

我在评论中说:


基本上,您需要做的是将会话数组分配给post数组,然后将该会话传递给其他页面(并在WHERE子句),然后在注销时销毁会话。

因此,这是您可以使用的逻辑基本示例:

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

边注:

最有可能需要我花费更多时间浏览整个代码,修理它。

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.

销毁会话的参考:

  • http://php.net/manual/en/function.session-destroy.php

这篇关于尝试将两个数据库连接到php文件并存储用户会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆