简体   繁体   English

如何在PHP中将会话变量传递到下一页

[英]how to pass session variable to next page in PHP

I am setting username of a user as a session and then printing that at index page but is nothing displays.. my login.php我将用户的用户名设置为会话,然后在索引页打印它,但没有显示任何内容..我的login.php

$user = $_POST["username"];
$_SESSION["username"]=$user;  // session started with username if I echoed this here it displaying correctly..
header('Location: index.php');
exit();

after login I'm redirecting the page to index.php and it is as below :登录后,我将页面重定向到index.php ,如下所示:

<?php session_start();
include("connection.php");
print_r($_SESSION); // print_r() displays nothing ???
?>

On executing above code this doesn't display's anything inside print_r() why?在执行上面的代码时,这不会在 print_r() 中显示任何内容,为什么? and how can I resolve this and how should I print session value now ?我该如何解决这个问题,我现在应该如何打印会话值?

Use session_start();使用session_start(); also on login.php也在login.php

<?php
session_start();
$user = $_POST["username"];
$_SESSION["username"]=$user;
header('Location: index.php');
exit();

Note:笔记:

While developing, use display_errors to track any errors or warnings your script may have, in this case include("connection.php");在开发时,使用display_errors来跟踪您的脚本可能存在的任何errorswarnings ,在这种情况下为include("connection.php"); may have them and that's why " print_r() displays nothing ??? ".可能有它们,这就是为什么“ print_r() 什么都不显示??? ”。
To enable error reporting on php code, append error_reporting(E_ALL); ini_set('display_errors', '1');要在 php 代码上启用错误报告,请附加error_reporting(E_ALL); ini_set('display_errors', '1'); error_reporting(E_ALL); ini_set('display_errors', '1'); at the top of your script.在脚本的顶部。

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

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