PHP表单使用会话提交后显示链接

时间:2013-12-02 02:44:49

标签: php session

我对此表单的目标是使用SESSION(我已经完成了w3school网站的COOKIE部分),以便用户使用简单的PHP无数据库登录,但这样做可以用户登录后只有一个会话,点击“记住”我。无论如何,我只是想证明用户只能访问一次链接,但是当他/她点击“记住”时仍会记住它们。 我试图刷新浏览器,但链接不会消失。示例,

初始表格显示:

Name: _________
Email: __________  () Remember   SUBMIT

点击“记住”后,应显示输入的姓名和电子邮件。

John
john@email.com

Here is your link: <some link>

当您刷新浏览器时,链接应该会消失,但会记住输入的姓名和电子邮件。

John
john@email.com

这是代码......任何想法?

// Session Starts
session_start();

// Vaidation, Sanitation and if the form is submitted



 if (!$fnameErr && !$lnameErr && !$emailErr && !$phoneErr && !$subjectErr  && !$messageErr)  {

   $_SESSIOIN['views'] = 1;
   if(isset($_SESSION['views']))
     $_SESSION['views']=$_SESSION['views']+1;
   }


    // SESSION
    if (isset($_POST['remember'])) {
  $customer_name = $_SESSION['name'];
if (!($customer_name)) {
  $customer_name = $_POST['name'];
}
  $customer_email = $_SESSION['email'];
if (!($customer_email)) {
  $customer_email = $_POST['email'];
}

    }


     if( ($visits > 2) || ($_SESSION['visits'] > $expire)) { 
     echo "Sorry you've downloaded your the file already.";         }
     else {
     echo '<a href="download.php"><b>Download our PDF brochure!</b></a>';        
     }              


    } else {
        echo '<div id="error">' . $errors . '<br /></div>';
    }



?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name:
<?php
  if (isset($_SESSION['name'])) {
    echo $_SESSION['name']."<br />";
  }
else {
?> 
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="25" /><br />
<?php } ?>

Email:
<?php
  if (isset($_SESSION['email'])) {
    echo $_SESSION['email']."<br /><br />";

  }
else {
?>  
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="25"/> 
<input type="checkbox" name="remember" /> Remember me
<input type="submit" name="Submit" />
<?php } ?>


</form>
</div>

1 个答案:

答案 0 :(得分:0)

我刚刚提出的是,如果$_SESSION['views'] < 2将显示链接,但不会更多地显示链接。

if(isset($_SESSION['views'])){
  $_SESSION['views']=$_SESSION['views']+1;  

}
else {
  $_SESSION['views']=1;
}  


if ($_SESSION['views'] < 2) {
  echo "Here's your link"; 
}
else {
echo "You have viewed this about ". $_SESSION['views']." times already."; 
}