Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

Unable to use $_SESSION in an other file (PHP)

Hello good afternoon, I'm having the trouble of not being able to use add a $_POST value to my $_SESSION variable so that I can use it later in my profile file.

  • I can give it other values like "hello" outside the if and it echoes hello in levraiprofilewesh.php.
  • I've tried making $_SESSION a super global variable and global but it didn't work.
  • I've been stuck on it for a few hours :(.
  • I don't need to upload the website on the web, it's just for local use, so there's no need for privacy feedback although it has helped me for the future.

login.php code:

<?php
session_start();   
include("db_connect.php"); 

if(isset($_POST['login_button'])) {
    $user_email = trim($_POST['user_email']);
    $user_password = trim($_POST['password']);
    $_SESSION['password'] = $user_password;
    $usql = "SELECT * FROM users WHERE Email='$user_email' && Password='$user_password'";
    $uresult = mysqli_query($db, $usql) or die("database error:". mysqli_error($db));
    $urow = mysqli_fetch_assoc($uresult);
    //while($row = mysqli_fetch_array($uresult)) 
    //$STPMARCHEFRERE = $row['Firstname'];
    //$SESSION = $row['Firstname'];
    
    if($urow['Password']==$user_password){
        setcookie("userid",$user_password,time()+(60*60*24*7));
        setcookie("useremail",$user_email,time()+(60*60*24*7));
        //$GLOBALS['SESSION'] = $user_password;
        $time=time();
        $queryz = "UPDATE Users 
                    Set Online='Online',
                        Time='$time' 
                    WHERE Password='$user_password' ";                        
        $db->query($queryz) or die('Errorr, query failed to upload');   
        echo "ok";   
    } else {                
        echo "email or password does not exist."; // wrong details 
    }       
}
?>

levraiprofilewesh.php code:

<?php
session_start(); 
include("login.php");
$connection = mysqli_connect('localhost', 'root','','phpchart');
if(isset($_SESSION['password'])) {
    $username = $_SESSION['password'];
    $query = "SELECT * 
                FROM users 
                WHERE Password = '%$username%'";

    $select_user_profile_query = mysqli_query($connection, $query);

    while($row = mysqli_fetch_array($select_user_profile_query)) {
        $post_tamere = $row['Firstname'];
        $post_tondaron = $row['Sirname'];
        $post_tasoeur = $row['Phone'];
        $post_tonneveu = $row['Institution'];
        $post_julia = $row['Email'];    
    }
}
?>
    
    
<?php 
if(isset($_POST['edit_user'])) {
    $user_firstname = $_POST['user_firstname'];
    $user_lastname = $_POST['user_lastname'];
    $user_role = $_POST['user_role'];
    
    //$post_image = $_FILES['image']['name'];
    //$post_image_temp = $_FILES['image']['tmp_name'];
    
    $username = $_POST['username'];
    $user_email = $_POST['user_email'];
    $user_password = $_POST['user_password'];
    //$post_date = date('d-m-y');
    //move_uploaded_file($post_image_temp, "./images/$post_image" );
    
    $query = "SELECT randSalt FROM users";
    $select_randsalt_query = mysqli_query($connection, $query);
    if(!$select_randsalt_query) {
        die("Query Failed" . mysqli_error($connection));
    }
       
    $row = mysqli_fetch_array($select_randsalt_query); 
    $salt = $row['randSalt'];
    $hashed_password = crypt($user_password, $salt);
 
    $query = "UPDATE users SET ";
    $query .="user_firstname  = '{$user_firstname}', ";
    $query .="user_lastname = '{$user_lastname}', ";
    $query .="user_role   =  '{$user_role}', ";
    $query .="username = '{$username}', ";
    $query .="user_email = '{$user_email}', ";
    $query .="user_password   = '{$hashed_password}' ";
    $query .= "WHERE username = '{$username}' ";
          
    $edit_user_query = mysqli_query($connection,$query);
       
    confirmQuery($edit_user_query);
}    
?> 
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
    <div id="wrapper">

        <!-- Navigation -->
        <div id="page-wrapper">
            <div class="container-fluid">
            <!-- Page Heading -->
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">Welcome to Profile
                            <small>Author</small>
                        </h1>
                        <form action="" method="post" enctype="multipart/form-data">    
                            <div class="form-group">
                                <label for="title">Firstname</label>
                                <input type="text" value="<?php echo $post_tamere; ?>" class="form-control" name="user_firstname">
                            </div>
                            <div class="form-group">
                                <label for="post_status">Lastname</label>
                                <input type="text" value="<?php echo $post_tondaron; ?>" class="form-control" name="user_lastname">
                            </div>
                            <div class="form-group">
                                <select name="user_role" id="">
                                    <option value="subscriber"><?php echo $user_role; ?></option>
<?php 

    if($user_role == 'admin') {
        echo "<option value='subscriber'>subscriber</option>";
    } else {
        echo "<option value='admin'>admin</option>";
    }

?>
                                </select>
                            </div>
          
                        <!--<div class="form-group">
                                 <label for="post_image">Post Image</label>
                                  <input type="file"  name="image">
                            </div>-->

                            <div class="form-group">
                                <label for="post_tags">Phone</label>
                                <input type="text" value="<?php echo $post_tasoeur; ?>" class="form-control" name="username">
                            </div>
      
                            <div class="form-group">
                                <label for="post_content">Email</label>
                                <input type="email" value="<?php echo $post_tonneveu; ?>" class="form-control" name="user_email">
                            </div>
                            
                            <div class="form-group">
                                <label for="post_content">Password</label>
                                <input type="password" value="<?php echo $post_julia; ?>" class="form-control" name="user_password">
                            </div>
                        <h1><?php echo $_SESSION['superhero']; ?></h1>

                        <div class="form-group">
                            <input class="btn btn-primary" type="submit" name="edit_user" value="Update Profile">
                        </div>
                    </form>
                </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </div>

 <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
        
question from:https://stackoverflow.com/questions/65845249/unable-to-use-session-in-an-other-file-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...