如何从PHP中的不同页面检索会话信息?

时间:2016-04-03 19:25:43

标签: php html mysql

这是显示输入数据的位置。目前它只显示电子邮件,它不会显示电话,城市和县。我想为特定用户显示所有这些信息

    <?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>";
        }


}

?>

2 个答案:

答案 0 :(得分: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 :(得分: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']或类似东西的地方,并扩展到你想要的值。 你发布的代码不符合它。