我有這個代碼在我的網頁的所有頁面顯示用戶名。如何在不重新登錄的情況下更新會話變量?
<p>Welcome <?php echo $_SESSION['username']; ?>!</p>
而我允許用戶更新他們的用戶名,但是當他們更新它。它不會更新會話變量,用戶必須重新登錄才能看到更改。 有什麼辦法可以不用重新登錄就更新它?
下面是一個使用更新
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();
echo $_SESSION['username'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Secure Login: Protected Page</title>
<link rel="stylesheet" href="styles/main.css" />
</head>
<body>
<?php if (login_check($mysqli) == true) : ?>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
<a href="logout.php">Log out</a>
<?php
mysql_connect("localhost","root");
mysql_select_db("userdata");
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
?>
<?php
if(isset($_GET['user_id']))
{
$result_set=mysql_query($sql_query);
$fetched_row=mysql_fetch_array($result_set);
}
if(isset($_POST['btn-update']))
{
// variables for input data
$username = $_POST['username'];
$email = $_POST['email'];
$user_id = $_SESSION['user_id'];
// variables for input data
// sql query for update data into database
$sql_query = "UPDATE users SET username='$username',email='$email' WHERE id= '$user_id'";
// sql query for update data into database
// sql query execution function
if(mysql_query($sql_query))
{
?>
<script type="text/javascript">
alert('Data Are Updated Successfully');
window.location.href='publish_page.php';
</script>
<?php
}
else
{
?>
<script type="text/javascript">
alert('error occured while updating data');
</script>
<?php
}
// sql query execution function
}
if(isset($_POST['btn-cancel']))
{
header("Location: publish_page.php");
}
?>
<center>
<div id="header">
<div id="content">
<label>PHP PHP Update Data From MySql - By Cleartuts</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
<td><input type="text" name="username" placeholder="username" value="<?php echo $_SESSION['username'];?>" required /></td>
</tr>
<tr>
<td><input type="text" name="email" placeholder="email" value="<?php echo $_SESSION['email'];?>" required /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
<button type="submit" name="btn-cancel"><strong>Cancel</strong></button>
</td>
</tr>
</table>
</form>
</div>
</div>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>
</body>
</html>
我已經嘗試過刪除緩存和didn't解決代碼I'm。任何想法如何我可以自動更新它?
'mysql_ *'函數被棄用,不建議使用它們,因爲它會讓您的代碼對sql注入打開...另外,我無法看到您嘗試更新會話變量的位置 - 它應該在成功更新db之後。 – RamRaider