如何从会话中存储一些东西

时间:2015-02-04 05:32:30

标签: php html mysql

我的网站上有一个登录系统。在用户登录时会话开始。当用户注册他的信息存储在userdb.php。现在我想在用户点击链接www.stackoverflow.com时存储用户的点并将其保存到我的数据库或userdb.php.再次显示点。 我的注册码是

<?php
if (session_id() == "")
{
   session_start();
}
$database = './usersdb.php';
$success_page = './Inside.php';
$error_message = "";
if (!file_exists($database))
{
   die('User database not found!');
   exit;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form_name'] == 'signupform')
{
   $newusername = $_POST['username'];
   $newemail = $_POST['email'];
   $newpassword = $_POST['password'];
   $confirmpassword = $_POST['confirmpassword'];
   $newfullname = $_POST['fullname'];
   $code = 'NA';
   if ($newpassword != $confirmpassword)
   {
      $error_message = 'Password and Confirm Password are not the same!';
   }
   else
   if (!preg_match("/^[A-Za-z0-9_!@$]{1,50}$/", $newusername))
   {
      $error_message = 'Username is not valid, please check and try again!';
   }
   else
   if (!preg_match("/^[A-Za-z0-9_!@$]{1,50}$/", $newpassword))
   {
      $error_message = 'Password is not valid, please check and try again!';
   }
   else
   if (!preg_match("/^[A-Za-z0-9_!@$.' &]{1,50}$/", $newfullname))
   {
      $error_message = 'Fullname is not valid, please check and try again!';
   }
   else
   if (!preg_match("/^.+@.+\..+$/", $newemail))
   {
      $error_message = 'Email is not a valid email address. Please check and try again.';
   }
   else
   if (isset($_POST['captcha'],$_SESSION['random_txt']) && md5($_POST['captcha']) == $_SESSION['random_txt'])
   {
      unset($_POST['captcha'],$_SESSION['random_txt']);
   }
   else
   {
      $error_message = 'The entered code was wrong.';
   }
   $items = file($database, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
   foreach($items as $line)
   {
      list($username, $password, $email, $fullname) = explode('|', trim($line));
      if ($newusername == $username)
      {
         $error_message = 'Username already used. Please select another username.';
         break;
      }
   }
   if (empty($error_message))
   {
      $file = fopen($database, 'a');
      fwrite($file, $newusername);
      fwrite($file, '|');
      fwrite($file, md5($newpassword));
      fwrite($file, '|');
      fwrite($file, $newemail);
      fwrite($file, '|');
      fwrite($file, $newfullname);
      fwrite($file, '|1|');
      fwrite($file, $code);
      fwrite($file, "\r\n");
      fclose($file);
      $subject = 'Your new account';
      $message = 'A new account has been setup.';
      $message .= "\r\nUsername: ";
      $message .= $newusername;
      $message .= "\r\nPassword: ";
      $message .= $newpassword;
      $message .= "\r\n";
      $header  = "From: webmaster@yourwebsite.com"."\r\n";
      $header .= "Reply-To: webmaster@yourwebsite.com"."\r\n";
      $header .= "MIME-Version: 1.0"."\r\n";
      $header .= "Content-Type: text/plain; charset=utf-8"."\r\n";
      $header .= "Content-Transfer-Encoding: 8bit"."\r\n";
      $header .= "X-Mailer: PHP v".phpversion();
      mail($newemail, $subject, $message, $header);
      header('Location: '.$success_page);
      exit;
   }
}
?>

请帮帮我......

2 个答案:

答案 0 :(得分:0)

要在会话中存储值,您只需使用以下语法

即可
$_SESSION['Desired variable name'] = value;

答案 1 :(得分:0)

你可以尝试使用ajax ..如果你想要保存点.. 就像你有一个链接

<a href="" id="stack-overflow">www.stackoverflow.com</a>
Now in your js file:
$('#stack-overflow').on('click', function() {
count = 1//how many points you want to assign
//send a ajax
$.ajax({
  url:'usersdb.php',
  type:'GET'
  dataType: 'json'
  data: {
   points: count,
      },
  success: function(resp){
  $('#stack-overflow').href='www.stackoverflow.com';
  //now triggered this link
  $('#stack-overflow').click();
  //or what ever msg you want to display
  });
  });

//现在你可以在usersdb.php中获得$ _POST值并保存它

如果还有其他问题,请确认...然后告诉我你想要的是什么。 感谢。