PHP会话已设置但在不同页面中无法访问

I am using php session for accessing various values in different pages. Session is set but its not being accessible in different pages.

below is the code which i am using

in validation.php

session_start();
$_SESSION["username"] = $username;
$_SESSION["user_id"]  = $user_id;

in profile.php

session_start();
$username = $_SESSION["username"];
$user_id = $_SESSION["user_id"];

but both the variables return null value but when i am using below code in validation.php is shows session is set

echo "<pre>";
print_r($_SESSION);

This gives proper output in validation.php but its not being accessed at profile.php

i have tried with

error_reporting(E_ALL);

after

session_start(); 

but nothing is happening.

Please help me out this.

The session set in validation.php is accessible in profile.php as you have mentioned session_start(); before accessing. There is no issue in way of access. You have to redirect the page after setting session value to make it accessible by other pages. If you are accessing profile.php through redirection it will print and if through ajax then it will not print. Once after redirection it will work and print.