PHP传递变量

时间:2016-08-25 20:01:58

标签: php html

我正在尝试将用户名值转到另一个页面,但不能。我的PHP代码:

 include("config.php");
 include('userClass.php');
 $userClass = new userClass();

 if (!empty($_POST['loginSubmit'])) 
 {
    $usernameEmail=$_POST['usernameEmail'];
    $password=$_POST['password'];


    if(strlen(trim($usernameEmail))>1 && strlen(trim($password))>1 )
    {
        $uid=$userClass->userLogin($usernameEmail,$password);
        if($uid)
        {
            $url=BASE_URL.'test.php';
            header("Location: $url"); // Page redirecting to home.php 
        }
    else
    {
        $errorMsgLogin="Please check login details.";
        }
    }
}

 <div id="login">
 <h3>Login</h3>
 <form method="post" action="" name="login">
    <label>Username or Email</label>
    <input type="text" name="usernameEmail" autocomplete="off" />
    <label>Password</label>
    <input type="password" name="password" autocomplete="off"/>
    <div class="errorMsg"><?php echo $errorMsgLogin; ?></div>
    <input type="submit" class="button" name="loginSubmit" value="Login">
   </form>
</div>

我想在新的php文件welcome.php中获取usernameEmai l我正在尝试$username = $_POST['usernameEmail'];,但它无效。任何指导都会有所帮助。

这是我的userclass.php

   class userClass
{
/* User Login */
public function userLogin($usernameEmail,$password)
{
    try{
    $db = getDB();
    $hash_password= hash('sha256', $password); //Password encryption 
    $stmt = $db->prepare("SELECT uid FROM doctorusers WHERE (username=:usernameEmail or email=:usernameEmail) AND password=:hash_password"); 
    $stmt->bindParam("usernameEmail", $usernameEmail,PDO::PARAM_STR) ;
    $stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR) ;
    $stmt->execute();
    $count=$stmt->rowCount();
    $data=$stmt->fetch(PDO::FETCH_OBJ);
    $db = null;
    if($count)
    {
        $_SESSION['uid']=$data->uid; // Storing user session value
        return true;
    }   
    else
    {   
        return false;
        } 
    }
    catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }

}

2 个答案:

答案 0 :(得分:0)

你可以通过插入

来使用php $ _SESSION
$_SESSION["mail"] = $usernameEmail;
表单成功后

并将会议开始放在页面顶部

session_start();

这会将您的用户名电子邮件保存到oupout use

echo $_SESSION["mail"];

确保你插入

  

在session_start();

每页顶部的

将使用$_SESSION["mail"];

你可以使用本教程作为例子

<?php
session_start();
    if (!empty($_POST['loginSubmit'])){
        $usernameEmail= $_POST['usernameEmail'];
        if(!empty($usernameEmail)){
            $_SESSION["mail"] = $usernameEmail;
        }
    }
?>
<?php 
if(!empty($_SESSION["mail"])){ 
echo $_SESSION["mail"];
}
?>
 <div id="login">
 <h3>Login</h3>
 <form method="post" action="" name="login">
    <label>Username or Email</label>
    <input type="text" name="usernameEmail" autocomplete="off" />
    <label>Password</label>
    <input type="password" name="password" autocomplete="off"/>
    <div class="errorMsg"></div>
    <input type="submit" class="button" name="loginSubmit" value="Login">
   </form>
</div>

答案 1 :(得分:0)

首先将session_start();添加到php标签的顶部。

将表单操作设置为action="welcome.php"

然后,您想要发送变量的时间和地点执行此$_SESSION['username'] = $usernameEmail

最后在welcome.php添加此行session_start();并使用$usernameEmail = $_SESSION['username'];调用您的变量,现在您可以使用$usernameEmail使用用户名执行任何操作

干杯