2016-11-22 136 views
0

我有这个代码在我的网页的所有页面显示用户名。如何在不重新登录的情况下更新会话变量?

<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。任何想法如何我可以自动更新它?

+0

'mysql_ *'函数被弃用,不建议使用它们,因为它会让您的代码对sql注入打开...另外,我无法看到您尝试更新会话变量的位置 - 它应该在成功更新db之后。 – RamRaider

回答

0

简单的解决办法:这应该从当你再次更新

if(mysql_query($sql_query)) 
    { 
    $_SESSION['username']=$username 
     ?> 
     <script type="text/javascript"> 
     alert('Data Are Updated Successfully'); 
     window.location.href='publish_page.php'; 
     </script> 
     <?php 
    } 
其他问题

阅读了关于预处理语句和SQL注入你脆弱的同时检查XSS保护您的用户名上面的代码不工作为名有需要的安全更新它只有你的问题的答案。

MySQL是过时,所以你有很多追赶做

+0

非常感谢你,它的工作原理。我正在完成构建我的网站,我很快就没有明智的数据。所以安全现在不是我的首要任务,而是功能性。接下来我会寻找安全。 –

0

你为什么不只是写新的价值$ _SESSION的SUCESSFUL更新:)后? $ _SESSION不是只读的。你只需要的更新,你自己。

相关问题