简体   繁体   中英

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. 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

    <?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

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

i am unsure why $se which is a int does not get passed to test.php

any help? and yes there are some html from stuff not included, but that is not related to the problem at hand

You're doing it wrong. You have sessions so use them:

$_SESSION['se'] = $me;

and then test.php would look like this:

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

You can refer any PHP variable like this. If you want to use preserved value of PHP variable or even any web language you must save it in to SESSION or COOKIE. In user login case, you should use SESSION variable. In your code start session and instead of $e define $_SESSION['e'] and access it any php script of your directory. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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