首页 > 解决方案 > PHP 从另一个 php 文件调用变量

问题描述

我有 3 个 php 文件。test.php 用于创建电子邮件的用户表单。process.php 用于处理验证和保存输入。最后一个registration2.php 是第二个页面,用上一节的输入来欢迎用户。我正在尝试将变量调用到此页面上,但似乎变量未保存或为空。我似乎无法弄清楚为什么?

测试.php

    <?php if(!isset($firstName)){
    $firstName='';
}if(!isset($lastName)){
    $lastName='';
}if(!isset($userName)){
    $userName='';
}if(!isset($password)){
    $password='';
}if(!isset($valPassword)){
    $valPassword='';
}
?>
<html>
<head>
    <title>Registration</title>
</head>
<body>
    <h1> Create your Google Account </h1>
    <form action="process.php" method="post">
        
        <input name="firstName" type="text" placeholder="First name" value="<?php echo htmlspecialchars($firstName)?>">
        <input name="lastName" type="text" placeholder="Last name" value="<?php echo htmlspecialchars($lastName)?>">
        <?php if (isset($firstName_error)){?>
            <p><?php echo $firstName_error ?></p>
        <?php }?>
        <?php if (isset($lastName_error)){?>
            <p><?php echo $lastName_error ?></p>
        <?php }?>
        <br>
        <br>
        <input name="userName" type="text" size="46" placeholder="username@gmail.com" value="<?php echo htmlspecialchars($userName)?>">
        <?php if (isset($userName_error)){?>
            <p><?php echo $userName_error ?></p>
        <?php }?>
        <p>You can use letters, numbers & periods&emsp;<input name="checkId" type="submit" value="Check ID" onclick="checkEmail();"></p>
        <br>
        <input name="password" type="text" placeholder="Password" value="<?php echo htmlspecialchars($password)?>">
        <input name="valPassword" type="text" placeholder="Confirm" value="<?php echo htmlspecialchars($valPassword)?>">
        <?php if (isset($password_error)){?>
            <p><?php echo $password_error ?></p>
        <?php }?>
        <?php if (isset($valPassword_error)){?>
            <p><?php echo $valPassword_error ?></p>
        <?php }?>
        <p> Use 8 or more characters with a mix of letters, numbers & symbols</p>
        
        <input name="next" type="submit" value="Next">
        
    </form> 

    
</body>
</html>

进程.php

<html>
<head>
    
</head>
<body>
<?php session_start();

    
    $firstName=$_POST["firstName"];
    $lastName=$_POST["lastName"];
    $userName=$_POST["userName"];
    $password=$_POST["password"];
    $valPassword=$_POST["valPassword"];
    $arrayPassword=str_split($password);
    $upper=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
    $lower=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
    $specialChar=["!","@","#","$","%","^","&"];
    $number=["0","1","2","3","4","5","6","7","8","9"];
    $counterU=0;
    $counterL=0;
    $counterS=0;
    $counterN=0;
    $existUser=[];
    $existUserInfo=[];
    




    for($i=0; $i<count($arrayPassword); $i++){
        for($j=0;$j<count($upper);$j++){
            if($arrayPassword[$i]==$upper[$j]){
                $counterU++;
            }else if($arrayPassword[$i]==$lower[$j]){
                $counterL++;
            }
            
        }
    }
    for($i=0; $i<count($arrayPassword); $i++){
        for($j=0;$j<count($specialChar);$j++){
            if($arrayPassword[$i]==$specialChar[$j]){
                $counterS++;
            }
            
        }
    }
    for($i=0; $i<count($arrayPassword); $i++){
        for($j=0;$j<count($number);$j++){
            if($arrayPassword[$i]==$number[$j]){
                $counterN++;
            }
            
        }
    }
    
    if(empty($firstName)){
        $firstName_error='Please insert your first name';
    }else{

    }
    if(empty($lastName)){
        $lastName_error='Please insert your last name';
    }
    if(empty($userName)){
        $userName_error='Please insert your username';
    }else if(strlen($userName)<18){
        $userName_error='Your username needs to have a minimum of 8 letters';
    }
    if(empty($password)){
        $password_error='Please insert your password';
    }else if(strlen($password)<8 || $counterU<2|| $counterL==0 ||$counterS==0 || $counterN==0){
        $password_error='The password must contain at least two uppercase
        characters, 1 lowercase character, 1 special character from {!, @, #, $, %, ^, &},
        1 digit and a minimum length of 8 characters';
    }
    if(empty($valPassword)){
        $valPassword_error='Please validate your password';
    }else if($password !== $valPassword){
        $valPassword_error='Your password does not match. Validate your password again';
    }
    include('test.php');

?>

<?php 
if(isset($_POST['checkId'])){
    if($userName!==$existUser && $userName!=''){
        array_push($existUserInfo, "$userName","$firstName","$lastName","$password","$valPassword");
        echo "this userID is available";
        
    }else if($userName==$existUser){
        $userName='';
        echo "This userID is not available. Please input a new userID";
    }
}

if(isset($_POST['next'])){
    if($firstName!=''&&$lastName!='' && $userName!=''){

        header('Refresh:1; URL=registration2.php');
    }
    
}

?>
</body>

</html>

注册2.php

<html>
<head>
    <title>Registration2</title>
</head>
<body>
    
    <form action="process.php" method="post">
        <h1> Create your Google Account </h1>
        <p><?php echo $firstName ?> ", welcome to google."</p>
        
    </form> 

    
</body>
</html>

标签: phphtml

解决方案


推荐阅读