2016-04-03 60 views
-1

這是顯示輸入數據的位置。目前,它只是顯示電子郵件,它不會顯示電話,城市和county.I想顯示所有這些信息爲用戶在其中輸入數據的特定用戶如何從PHP中的不同頁面檢索會話信息?

<?php 
    session_start(); 

    if(!($_SESSION['email'])){ 
     header("location: login.php"); 

    } 
    ?> 
    <html> 
    <head> 
    <title>Login Page</title> 
    </head> 
    <style type='text/css'> 
    body{ 
     background:url('wellcome.png'); 
    } 
    </style> 
    <body><b>Welcome you have succesfully logged into your account</b> 
    <p></p> 
    <font color='red' size='5'><?php echo $_SESSION['email'];?> 
    </font> 
    <font color='red' size='5'><?php echo $_SESSION['name'];?> 
    <font color='red' size='5'><?php echo $_SESSION['phone'];?> 
    <font color='red' size='5'><?php echo $_SESSION['city'];?> 
    <font color='red' size='5'><?php echo $_SESSION['county'];?> 
    </font> 
    <h1 align='right' style='margin-right:50px; margin-top:50px'> 
    <a href='logout.php'>Logout Here</a></h1> 
    </body> 
    </html> 

這就是:

<html> 

    <head> 
    <title>Registration Forma</title> 
    </head> 

    <body> 
    <form method='post' action='registration.php'> 
    <table width='400' border='5' align='center'> 
      <tr> 
      <td colspan = '5'><h1>Registration Form</h1></td> 

      </tr> 
      <tr> 
      <td align = 'center'>User Name:</td> 
      <td><input type='text' name='name'/></td> 
      </tr> 

      <td align = 'center'>Phone Number:</td> 
      <td><input type='text' name='phone'/></td> 
      </tr> 
      <td align = 'center'>County:</td> 
      <td><input type='text' name='county'/></td> 
      </tr> 
      <td align = 'center'>City/Town:</td> 
      <td><input type='text' name='city'/></td> 
      </tr> 
      <tr> 

      <td align = 'center'>User Paswword:</td> 
      <td><input type='password' name='pass'/></td> 
      </tr> 

      <tr> 
      <td align = 'center'>Email:</td> 
      <td><input type ='text' name='email' /></td> 
      </tr> 

      <tr> 
      <td colspan='5' align='center'><input type='submit' name='submit' value='sign up' /></td> 
      </tr> 
    </table> 
    </form> 
    <center><B>Already Registered </b> <a href='login.php'>Login here</a></center> 
    </center> 
    </body> 
    </html> 

    <?php 

     $servername = "localhost"; 
    $username = "root"; $password = ""; 
    $dbname = "users_db"; 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    if (mysqli_connect_errno()) 
     { echo "MySQLi Connection was not established: " . mysqli_connect_error(); 
    } 
    if(isset($_POST['submit'])) 
    { $user_name = $_POST['name']; 
    $user_pass = $_POST['pass']; 
    $user_email = $_POST['email']; 
    $Phone = $_POST['phone']; 
    $County = $_POST['county']; 
    $City = $_POST['city']; 
    if($user_name=='') 
    { echo "<script>alert('Please enter your name!')</script>"; 
    exit(); } 
    if($user_pass=='') 
    { echo "<script>alert('Please enter your password!')</script>"; 
    exit(); } if($user_email=='') 
    { echo "<script>alert('Please enter your email!')</script>"; 
    exit(); } 
    //CHECKING FIELDS 
    $check_username = "SELECT * FROM users WHERE user_name = '$user_name'"; 
    $result_username= mysqli_query($conn, $check_username) or die (mysqli_error($conn)); 
    if (mysqli_num_rows($result_username) > 0) { 
     echo "Username already exists, please try another one!"; 
    exit(); 
    } 
    $check_email = "SELECT * FROM users WHERE user_email = '$user_email'"; 
    $result_email = mysqli_query($conn, $check_email) or die (mysqli_error($conn)); 
    if (mysqli_num_rows($result_email) > 0) 
    { echo "Email already exists, please try another one!"; 
    exit(); } 
    //INSERTING DATA TO DATABASE 
    $insertquery = "INSERT INTO users (user_name,user_pass,user_email,Phone,County,City) 
    VALUES ('$user_name','$user_pass','$user_email','$Phone','$County','$City')"; 
    if (mysqli_query($conn, $insertquery)) 
     { echo "<script>window.open('welcome.php','_self')</script>"; } else { 
      echo "Error: " . $insertquery . "<br>" . mysqli_error($conn); 
      } 
      } 

      ?> 

這是我的登錄頁面。這是用戶輸入他們的電子郵件和密碼登錄的地方。當用戶登錄進入歡迎頁面時,我想讓他們看到他們的個人信息,例如姓名,電話,城市和縣。

<?php 
session_start() 

?> 

<html> 
<head> 
<title>Login Page</title> 
</head> 
<style type='text/css'> 
body{ 
    background:url('back.jpg'); 
} 
</style> 
<body> 
<form method='post' action='login.php'> 
<table width='400' border='5' align='center'> 
     <tr> 
     <td colspan = '5'><h1>Login Form</h1></td> 

     </tr> 

     <tr> 

     <td align = 'center'>Paswword:</td> 
     <td><input type='password' name='pass'/></td> 
     </tr> 

     <tr> 
     <td align = 'center'>Email:</td> 
     <td><input type ='text' name='email' /></td> 
     </tr> 

     <tr> 
     <td colspan='5' align='center'><input type='submit' name='login' value='login' /></td> 
     </tr> 
</table> 
</form> 
<center> 
<font color='red' size='6'>registered yet? 
</font><a href ='registration.php'>Sign up here</a> 
</body> 
</html> 

<?php 
$servername = "localhost"; 
$username = "root"; $password = ""; 
$dbname = "users_db"; 
$conn = new mysqli($servername, $username, $password, $dbname); 

if(isset($_POST['login'])){ 
    $password = $_POST['pass']; 
    $email = $_POST['email']; 


    $check_email = "select * from users where user_pass='$password' AND user_email='$email'"; 


    $run = mysqli_query($conn, $check_email) or die (mysqli_error($conn)); 

    if (mysqli_num_rows($run) > 0) 

{

$data = mysqli_fetch_array($check_email); 

$_SESSION['email']=$data['user_email']; 
$_SESSION['name']=$data['user_name']; 
$_SESSION['phone']=$data['Phone']; 
$_SESSION['county']=$data['County']; 
$_SESSION['city']=$data['City']; 

     echo 
     "<script>window.open('welcome.php','_self')</script>"; 
     } 
     else{ 
     echo "<script>alert('Email or password is incorrect!')</script>"; 
     } 


} 

?> 
+0

我沒有看到你設置了會話的位置 – Mihai

+0

**這是你的意思嗎?這是從我的登錄頁面** if(isset($ _ POST ['login'])){ \t $ password = $ _POST ['pass']; \t $ email = $ _POST ['email']; \t \t \t $ check_email =「select * from user where user_pass ='$ password'AND user_email ='$ email'」; \t \t \t $ run = mysqli_query($ conn,$ check_email)or die(mysqli_error($ conn)); \t如果(mysqli_num_rows($運行)> 0) \t { \t \t $ _SESSION [ '電子郵件'] = $電子郵件; \t \t \t \t \t 回聲\t \t \t 「」; \t \t} \t其他{\t \t \t回聲 「」; \t \t} \t } > – artur

+0

電子郵件均田,你只設置會話這裏'$ _SESSION [「電子郵件」] = $電子郵件;'做所有其他人一樣 – Mihai

回答

0

,試圖在PHP開始加入使用error_reporting:

<?php 
    error_reporting(-1); 
    session_start(); 

這將顯示發生的任何錯誤, 我猜測,問題就出在登錄頁面。您很可能只將電子郵件值設置爲會話變量,而不是其他值。

如果你添加error_reporting它會告訴你什麼是錯的。

你會發現你設置$_SESSION['email']代碼並添加代碼到其他屬性添加到$_SESSION變量

if(isset($_POST['login'])){ 
$password = $_POST['pass']; 
$email = $_POST['email']; 

$check_email = "select * from users where user_pass='$password' AND user_email='$email'"; 


$run = mysqli_query($conn, $check_email) or die (mysqli_error($conn)); 

if (mysqli_num_rows($run) > 0) 
{ 
    // here is where you need to add all the other variables but you need to retrieve them from the mysql request and not the post data which you set above 
    $data = mysqli_fetch_array($check_email); 
    // now i don't know the column names for these but i'm guessing they are user_variable 
    $_SESSION['email']=$data['user_email']; 
    $_SESSION['name']=$data['user_name']; 
    $_SESSION['phone']=$data['user_phone']; 
    $_SESSION['city']=$data['user_city']; 
    $_SESSION['county']=$data['user_county']; 


    echo 
    "<script>window.open('welcome.php','_self')</script>"; 
    } 
    else{ 
    echo "<script>alert('Email or password is incorrect!')</script>"; 
    } 

}

+1

儘管有用,但應該是一條評論。它不回答這個問題。 – ceejayoz

+0

@ceejayoz它應該/絕對可以讓他回答,如果他得到錯誤信息,他可以編輯他的問題,我會進一步編輯我的答案?或者這不是一個正確的方法來處理這個? – Jester

+0

我試過了,我在頁面上得到了這個回覆:注意:未定義的索引:在D行中的D:\ xampplite \ htdocs \ registration \ welcome.php上的名稱 注意:未定義索引:phone in D:\ xampplite \ htdocs \ registration \ welcome.php on line 24 注意:Undefined index:D:\ xampplite \ htdocs \ registration \ welcome.php on line 25 注意:未定義索引:D:\ xampplite \ htdocs \ \ welcome.php 26行 – artur

0

我無法想象你的意思是「不同的頁面「確切地說。 這兩個網頁是否存儲在同一個網絡服務器(例如:mywebsite.com/pageA和mywebsite.com/pageA)或不同的網絡服務器(例如:google.com和bing.com)上?

如果您想使用$ _SESSION [] - http://php.net/manual/en/reserved.variables.session.php 僅適用於同一網站。

此外,你需要指定的地方(可能在checkLogin頁/功能)從數據庫的值到$ _SESSION值。 也許你在尋找$ _SESSION ['email'] = $ databaserow ['email']或類似的地方,並擴展到你想要的值。 您發佈的代碼與此無關。

+0

其他頁面我的意思是用戶登錄頁面就像他們的個人資料頁面並顯示他們的個人信息 – artur