Abhijit Kumbhar
Abhijit Kumbhar

Reputation: 961

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

$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 :

<?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? and how can I resolve this and how should I print session value now ?

Upvotes: 1

Views: 4138

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 99051

Use session_start(); also on 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");may have them and that's why "print_r() displays nothing ???".
To enable error reporting on php code, append error_reporting(E_ALL); ini_set('display_errors', '1'); at the top of your script.

Upvotes: 1

Related Questions