2016-12-16 126 views
0

我想調用一個變量的值到我的其他php文件,但它不工作。首先,它向我展示了除了新的數據之外的舊數據,但現在它甚至沒有獲得任何價值。這是我在第一個文件

$_SESSION['tsmUserName']=$username; 

做這是我做的第二

$ids=$_SESSION['tsmUserName']; 

我竟然用的var_dump,它只是告訴我,tsmusername是不確定的。當我第一次製作程序時一切正常,但後來我將主鍵更改爲用戶名,這一切都搞砸了,PHP表單開始使用舊的刪除值自動填充輸入字段,然後當我將主鍵更改回它仍然開始顯示舊的刪除值。重新啓動我的計算並再次檢查後,它變得更糟,因爲現在我得到未定義的錯誤。這裏,r我的兩個PHP文件的代碼,以防萬一:

<?php 
     $firstnameError = $lastnameError = $usernameError = $passwordError = $birthdateError = $mobileError = $emailError = ""; 
    // Check if data are posted. 
    if (isset($_POST['submit'])){ 
     $firstname = $lastname = $username = $password = $birthdate = $mobile = $email = ""; 
     $errorOccured = false; 

     if (isset($_POST['tsmFirstName'])){ 
      $firstname = trim($_POST['tsmFirstName']); 
      if (strlen($firstname) == 0){ 
       $firstnameError = "First Name is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $firstnameError = "First Name is missing"; 
     } 

     //Checking last name 
     if (isset($_POST['tsmLastName'])){ 
      $lastname = trim($_POST['tsmLastName']); 
      if (strlen($lastname) == 0){ 
       $lastnameError = "First Name is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $lastnameError = "First Name is missing"; 
     } 

     //checking the username 
     if (isset($_POST['tsmUserName'])){ 
      $username = trim($_POST['tsmUserName']); 
      $usernameLenght = strlen($username); 
      if ($usernameLenght == 0){ 
       $usernameError = "First Name is missing"; 
       $errorOccured = true; 
      } 
      elseif ($usernameLenght < 4){ 
       $usernameError = "Your username should be minimum 4 characters"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $usernameError = "First Name is missing"; 
     } 


     //checking the password 
     if (isset($_POST['tsmPassword'])){ 
      $password = trim($_POST['tsmPassword']); 
      if (strlen($password) == 0){ 
       $passwordError = "First Name is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $passwordError = "First Name is missing"; 
     } 

     //checking the birthdate 
     if (isset($_POST['tsmBirthDate'])){ 
      $birthdate = trim($_POST['tsmBirthDate']); 
      if (strlen($birthdate) == 0){ 
       $birthdateError = "First Name is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $birthdateError = "First Name is missing"; 
     } 

     // checking the mobile 
     if (isset($_POST['tsmMobile'])){ 
      $mobile = trim($_POST['tsmMobile']); 
      if (strlen($mobile) == 0){ 
       $mobileError = "First Name is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $mobileError = "Mobile is missing"; 
     } 

     //checking the email 
     if (isset($_POST['tsmEmail'])){ 
      $email = trim($_POST['tsmEmail']); 
      if (strlen($email) == 0){ 
       $emailError = "Email is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $emailError = "Email is missing"; 
     }              


     if(!$errorOccured){ 
      require_once("connection.php"); 

     $_SESSION['tsmUserName']=$username; 
     $names=$_SESSION['tsmFirstName']; 
     var_dump($_SESSION); 
      $keychain= sha1($username); 
      $my_query="INSERT INTO users (id, firstname, lastname, username, password, birthdate, mobile, email, status, keychain) VALUES (NULL,'$firstname','$lastname','$username','$password','2016-1-1','$mobile','$email','0','$keychain')"; 
      $result=mysqli_query($connection,$my_query); 
      if($result){ 
       $message_mail = "Dear $firstname, <br> Thank You for registering with us. Click now on the link below to activate your account. <br> $keychain <br><br> Thank You."; 
       $subject_mail= "Your account has been created"; 
       $to_mail = $email; 
       $headers[]= 'MIME-Version 1.0'; 
       $headers[]= 'content-type:text/html; charset=utf8'; 
       $headers[]= 'From: [email protected]'; 
       mail($to_mail,$subject_mail,$message_mail,implode("\r\n",$headers)); 
       //after email is sent redirect to thank you page. 
       header("Location:completed.php?task=done"); 
       exit(); 
       echo 'Thank You'; 
      } 
       else{ 
        echo "<b> Error unable to post. </b>"; 
        mysqli_error($connection); 
       } 
       mysqli_close($connection); 
      } 

     } 


?> 

而且應該從第一個獲取用戶名,第二個是在這裏

<?php 
session_start(); 
$EntryError=$ExitError=""; 
    if (isset($_POST['submit'])){ 
     $entrydate = $exitdate = ""; 
     $errorOccured = false; 

     if (isset($_POST['tsmdate'])){ 
      $entrydate = trim($_POST['tsmdate']); 
      if (strlen($entrydate) == 0){ 
       $EntryError = "date is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $EntryError = "date is missing"; 
     } 

     // checking for last name 
     if (isset($_POST['tsmexit'])){ 
      $exitdate = trim($_POST['tsmexit']); 
      if (strlen($exitdate) == 0){ 
       $ExitError = "First Name is missing"; 
       $errorOccured = true; 
      } 
     } 
     else{ 
      $ExitError = "last Name is missing"; 
     } 
     $ids=$_SESSION['tsmUserName']; 

     var_dump($_SESSION); 
     if(!$errorOccured){ 
      require_once("connection.php"); 
      $my_query="INSERT INTO timing (`No`, `Entry Date and Time`, `Exit Date and Time`, `Username`) VALUES (NULL,'$EntryError','$exitdate','$ids')"; 
      $result=mysqli_query($connection,$my_query); 
      if($result) 
      { 
       echo 'thank you'; 
      } 
      else 
      { 
       echo 'error'; 
      } 
      mysqli_close($connection); 
     } 
    } 
?> 
+4

你必須調用'session_start()'在這兩個文件 – WillardSolutions

+0

OMG多數民衆贊成它..我是這樣的我失去了主意!謝謝!!!!!!! –

回答

0

每個網頁需要訪問至$_SESSION必須首先調用session_start()函數。

根據Marcin的評論,這也可以通過將會話設置爲在php.ini文件中自動啓動而不使用每個頁面上的session_start()函數來實現。

session.auto_start=On 
+0

這個答案實際上是錯誤的措詞。正確的答案應該說'session_start()'需要在訪問'$ _SESSION'之前被調用,但是你可以通過[session.auto_start](http://php.net/manual/en/session.configuration)來處理。 php#ini.session.auto-start)也是php.ini的選項,你可以不用在你的代碼中調用'session_start()'。 –

+0

@marcin,謝謝你,修正了答案 – ryansin

0

正如@Sinfieldd前面提到的,你應該在每一個PHP腳本的一開始,以訪問會話陣列, 這樣做,添加以下代碼啓動會話:

session_start(); 
$_SESSION['key'] = "value"; 

// and clear the session when you're done, for example when the user logs out 
session_destroy(); 
0

你必須開始一個會話才能工作。要開始會話使用session_start()