Hi guys, when switched to the newer version of php i got some problems with sessions the scenario is file: login.php <?php session_start(); $_SESSION['Username'] = $Username = $_POST['login']; $_SESSION['Password'] = $Password = $_POST['password']; header("Location: proccess.php"); ?> now some another page file: parse.php <?php session_start(); $Username = $_SESSION['Username']; $Password = $_SESSION['Password']; echo $Username; echo "\n"; echo $Username; ?> now the problem is in parse.php when it's executed Notice: Undefined index: Username in C:\xampp\htdocs\clients\parse.php on line 3 Notice: Undefined index: Password in C:\xampp\htdocs\clients\parse.php on line 4 can anyone help me plz
You are getting that error most likely because the $_SESSION['Username'] variable/index doesn't exist in the session array. To avoid that error/notice, you'd want to check for that variable before using it. For example: if (!empty($_SESSION['Username'])) { $Username = $_SESSION['Username']; } Code (markup): Or if you are using a new version of PHP, you could do something like this when assigning variables: $Username = $_SESSION['Username'] ?: null; $Password = $_SESSION['Password'] ?: null; Code (markup): The same method could be used in your login.php script when accessing the $_POST variables.
You can use even isset function to check whether session variable is set or not if(isset($_SESSION['Username'])) { echo $_SESSION['username']; } Code (markup):