简体   繁体   English

PHP变量引用问题

[英]PHP variable referencing issue

I am trying to read the id of a user from the database when they log in, and save it to a variable to used in other programs for later. 我试图在用户登录时从数据库中读取用户的ID,并将其保存到变量中,以供以后在其他程序中使用。 my table for the users is such 我的用户表是这样的

    addressBookUsers
[
userid int(11) PK AUTO_INCREMENT;
firstName;
LastName;
email
]

with some dummy data 有一些虚拟数据

userid username password
1      fred     12ewerefds2
2      al        343ed3fe

this is the code in which i use the username to get the user id and store into a variable 这是我使用用户名获取用户ID并将其存储到变量中的代码

    <?php
    session_start();
    include("dbconnect.php");
    $con= new dbconnect();
    $con->connect();

    //create and issue the query
    $id = "SELECT userid FROM addressBookUsers WHERE username = '".$_POST["username"]."'";
    $userid = mysql_query($id);
    while($row = mysql_fetch_array($userid)) {
    $me = $row[0]}
    $se=$me;


    echo($se)
?>

this returns the correct user id however when i try to call $se in another php file to see if it has saved i dont get a resul 这将返回正确的用户ID,但是当我尝试在另一个php文件中调用$ se以查看是否已保存时,我没有得到resul

test.php
<?php
include ("userloginses.php");
echo $se;
?>

i am unsure why $se which is a int does not get passed to test.php 我不确定为什么$ se这是一个整数不会传递给test.php

any help? 有什么帮助吗? and yes there are some html from stuff not included, but that is not related to the problem at hand 是的,其中不包含一些来自html的内容,但这与当前的问题无关

You're doing it wrong. 你这样做是错的。 You have sessions so use them: 您有会议,因此使用它们:

$_SESSION['se'] = $me;

and then test.php would look like this: 然后test.php将如下所示:

<?php
    session_start();
    include ("userloginses.php");
    echo $_SESSION['se'];
?>

You can refer any PHP variable like this. 您可以像这样引用任何PHP变量。 If you want to use preserved value of PHP variable or even any web language you must save it in to SESSION or COOKIE. 如果要使用PHP变量或任何Web语言的保留值,则必须将其保存到SESSION或COOKIE中。 In user login case, you should use SESSION variable. 在用户登录的情况下,应使用SESSION变量。 In your code start session and instead of $e define $_SESSION['e'] and access it any php script of your directory. 在代码启动会话中,而不是$ e定义$ _SESSION ['e']并访问目录中的任何PHP脚本。 Don't forget to start session using session_start() in first line of your each and every php script where you want to access this variable. 不要忘记在每个要访问此变量的PHP脚本的第一行中使用session_start()启动会话。

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

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