mbrown Posted February 5, 2017 Share Posted February 5, 2017 <?php session_start(); if(empty($_SESSION['user_id'])){ header('location: index.php'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Main Dashboard</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="apple-mobile-web-app-capable" content="yes"> <link href="css/bootstrap.min.css" rel="stylesheet"> <link href="css/bootstrap-responsive.min.css" rel="stylesheet"> <link href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600" rel="stylesheet"> <link href="css/font-awesome.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> <link href="css/pages/dashboard.css" rel="stylesheet"> <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --> <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <?php $userid=$_SESSION['user_id']; echo $userid; ?> I have the above code. I am curious why I can not print out the userid on the page. I am using this as a roadmap as I want to include the username, first and last name in the session variables so I can use it throughout the current session to write out something like "Welcome First_Name Last_Name" Thanks for the help! Mike Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 5, 2017 Share Posted February 5, 2017 You have to start the session before you can use it. Putting all the user data into the session doesn't make much sense. Store the user ID and then look up the data in the database when needed. Quote Link to comment Share on other sites More sharing options...
mbrown Posted February 5, 2017 Author Share Posted February 5, 2017 All right. I have it tested with the userid as shown above in the example. Nothing is printed out. I am currently in the session and nothing. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 5, 2017 Share Posted February 5, 2017 technically, there's nothing that would prevent your code from working. however, i can name at least a half-dozen things that could make it 'appear' like it isn't working. do you have php's error_reporting set to E_ALL and display_errors set to ON, so that php would help you by reporting and displaying all the errors it detects. i'm betting you would be getting php errors that would help pin down the problem. next, what is the user_id value supposed to be? one of the possibilities for why it may not be 'appearing' is if it happened to intentionally or accidentally be in the form of a html tag, in which case it would appear in the 'view source' of the page and not be displayed. lastly, you need an exit; statement after the header() redirect to stop program execution. the current code allows a non-logged in user to access the page since all the code still runs. Quote Link to comment Share on other sites More sharing options...
mbrown Posted February 5, 2017 Author Share Posted February 5, 2017 technically, there's nothing that would prevent your code from working. however, i can name at least a half-dozen things that could make it 'appear' like it isn't working. do you have php's error_reporting set to E_ALL and display_errors set to ON, so that php would help you by reporting and displaying all the errors it detects. i'm betting you would be getting php errors that would help pin down the problem. next, what is the user_id value supposed to be? one of the possibilities for why it may not be 'appearing' is if it happened to intentionally or accidentally be in the form of a html tag, in which case it would appear in the 'view source' of the page and not be displayed. lastly, you need an exit; statement after the header() redirect to stop program execution. the current code allows a non-logged in user to access the page since all the code still runs. The user_id is a integer. ERROR REPORTING set to all DISPLAY ERRORS is enabled I am seeing it now. Not sure why it did not show up before. Thanks for the reminder about error reporting and display errors. That will help me out alot in the future in development. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.