简体   繁体   English

PHP从另一个.php文件获取变量

[英]PHP getting a variable from another .php file

I'm Alex and I am learning PHP. 我是Alex,正在学习PHP。 I have two files named.. file1.php and file2.php. 我有两个文件名为.. file1.php和file2.php。 I have a variable inside file1.php and I want to refer it in file2.php, how can I do that? 我在file1.php中有一个变量,我想在file2.php中引用它,我该怎么做? I know it's a stupid question for most of you but I'm new to this and I'm trying to learn. 我知道对于大多数人来说这是一个愚蠢的问题,但是我对此并不陌生,我正在尝试学习。 Thank you in advance, I'll provide you with my code. 预先谢谢您,我将为您提供代码。

File1.php File1.php

<?php

if(isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $passwordmd5 = md5($password);

    if(!empty($username) && !empty($password)){

        $query = "SELECT `id` FROM `users` WHERE `username` = '$username' AND `password` = '$passwordmd5'";

        if($queryrun = mysql_query($query)){
            $query_num_rows = mysql_num_rows($queryrun);
            if($query_num_rows == 0){
                echo 'Invalid username and password';
            } else if($query_num_rows == 1){
                echo $user_id = mysql_result($queryrun, 0, 'id');
                $_SESSION['user_id']=$user_id;
                header('Location: index.php' . $username);
            }
        }  else {
            echo 'Enter a username and a password';
        }
    }

?>

File2.php File2.php

<?php
        require 'core.php';
        require 'connectdb.php';

        if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
            echo '<a href = "logout.php"><input type = "submit" id = "logreg" value = "Logout" /></a>';
        }else{
            echo '<a href = "loginregister.php"><input type = "submit" id = "logreg" value = "Login/Register" /></a>';
        }
    ?>

Basically, File1.php is a login form for a website and I am entering a username and submit it, if I succesfully submit it and I login, I want my button to change from "Login/Register" to "Welcome, ", how can I do that? 基本上,File1.php是网站的登录表单,我输入用户名并提交,如果成功提交并登录,我希望我的按钮从“登录/注册”更改为“欢迎”,如何我能做到吗? On a short note, how can I transfer my $username variable that I enter in my File1.php to File2.php? 简而言之,如何将我在File1.php中输入的$ username变量转移到File2.php? For example.. I press the Login/Register button, I type in Alex as a username and 123 as password and press the Login button. 例如,我按下“登录/注册”按钮,我输入Alex作为用户名,输入123作为密码,然后按下“登录”按钮。 (That to be done in file1.php) Considering the user and the password being correct, I am moving to file2.php and instead of the Login/Register button, I want a new button, or a div (whatever) to show up with the text "Welcome, Alex" or "Welcome, -username-". (在file1.php中完成)考虑到用户名和密码正确,我将移至file2.php,而不是“登录/注册”按钮,而是要显示一个新按钮或一个div(无论如何)文字为“ Welcome,Alex”或“ Welcome,-username-”。 How can I do that? 我怎样才能做到这一点?

You need to include session_start on your file2.php code. 您需要在file2.php代码中包含session_start。 If file1.php isn't the first page you will need to add the session_start at the beginning of it as well. 如果file1.php不是第一页,则还需要在其开头添加session_start。

<?php
   session_start();
    require 'core.php';
    require 'connectdb.php';

    if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
        echo '<a href = "logout.php"><input type = "submit" id = "logreg" value = "Logout" /></a>';
    }else{
        echo '<a href = "loginregister.php"><input type = "submit" id = "logreg" value = "Login/Register" /></a>';
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM