0

I'm a beginner in PHP. I have an error which is

Undefined index: user_id on line 7.

The page is work but when I reload it's show me this error and gone. How can I fix it ?

<?php
if (isset($_GET['username']) === true && empty($_GET['username']) === false) {
    include 'core/init.php';
    $username    = $getFromU->checkInput($_GET['username']);
    $profileId   = $getFromU->userIdByUsername($username);
    $profileData = $getFromU->userData($profileId);
    $user_id     = $_SESSION['user_id'];
    $user        = $getFromU->userData($user_id);

    if (!$profileData) {
        header('Location: index.php');
    }
}
?>

The error on line 7 which means this line

$user_id     = $_SESSION['user_id'];

But I don't see any problem ? Someone help me please!``

The file of project: profile.php

this is init.php

<?php
include 'database/connection.php';
include 'classes/user.php';
include 'classes/tweet.php';
include 'classes/follow.php';

global $pdo;

session_start ();

$getFromU = new User($pdo);
$getFromT = new Tweet($pdo); 
$getFromF = new Follow($pdo);

define("BASE_URL", "http://localhost/twitter/");  

?>

Kx 2000x
  • 3
  • 6
  • 1
    Are you using `session_start()` in this file or in init.php? – ferhado Oct 25 '17 at 04:25
  • Share code where you set session ? – Niklesh Raut Oct 25 '17 at 04:26
  • yes i use session_start() in init.php – Kx 2000x Oct 25 '17 at 04:27
  • There are few possibilities with this case like , (i) Did you start the session (ii) Did you store the value in session for that variable ?? (iii) Did you properly stores the value in session ? Please check your code with all aspects for this .. –  Oct 25 '17 at 04:29
  • session is not set for `user_id`, so while accessing it is showing error. You can `print_r($_SESSION)` to verify it is having `user_id` or not – Jigar Shah Oct 25 '17 at 04:30
  • Just to chime in...Have you set $_SESSION['user_id'?], does it exist by the time you reach that line of code - Computer says No! – TimBrownlaw Oct 25 '17 at 04:32
  • print_r does not work or maybe i use it wrong , how can i use it correctly ? – Kx 2000x Oct 25 '17 at 04:35

0 Answers0