I am new to PHP and struggling with the following:
I have a page where I want to check if someone is a registered user before allowing them to see the contents of the site. Therefore, I thought that in my header file (which is indicated on all individual pages via require_once("includes/header.php"); ) I can check this and redirect them to the login page ( login.php ) if they have not already entered into the system.
So, here is all that I have in my header:
<!DOCTYPE html> <html> <head> <?php define("someUnguessableVariable", "anotherUnguessableVariable"); session_start(); if(!(isset($_SESSION['login']) && $_SESSION['login'] != '')){ header ("Location: login.php"); } include "system/config.php"; $pageURL = basename($_SERVER["REQUEST_URI"]); $pageName = pathinfo(parse_url($pageURL, PHP_URL_PATH), PATHINFO_FILENAME); $selectedLang = $_GET["lang"]; if(!isset($selectedLang)){ $selectedLang = "de"; } $langURL = "?lang=" . $selectedLang; $conn = new mysqli($dbServer, $dbUser, $dbPass, $dbName); $conn->set_charset("utf8"); if($conn->connect_error){ die("Connection failed: " . $conn->connect_error); } <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="author" content="Some author" /> <meta name="description" content="Created: 2015-06" /> <base href="http://www.myurl.de" target="_self" /> <title>Some title</title> <link rel="stylesheet" type="text/css" href="includes/styles.css" /> <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" /> <link rel="shortcut icon" href="images/favicon/favicon.ico" type="image/x-icon" /> <link rel="icon" href="images/favicon/favicon.png" type="image/png" /> <link rel="icon" sizes="32x32" href="images/favicon/favicon-32.png" type="image/png" /> <link rel="icon" sizes="64x64" href="images/favicon/favicon-64.png" type="image/png" /> <link rel="icon" sizes="96x96" href="images/favicon/favicon-96.png" type="image/png" /> <link rel="icon" sizes="196x196" href="images/favicon/favicon-196.png" type="image/png" /> <link rel="apple-touch-icon" sizes="152x152" href="images/favicon/apple-touch-icon.png" /> <link rel="apple-touch-icon" sizes="60x60" href="images/favicon/apple-touch-icon-60x60.png" /> <link rel="apple-touch-icon" sizes="76x76" href="images/favicon/apple-touch-icon-76x76.png" /> <link rel="apple-touch-icon" sizes="114x114" href="images/favicon/apple-touch-icon-114x114.png" /> <link rel="apple-touch-icon" sizes="120x120" href="images/favicon/apple-touch-icon-120x120.png" /> <link rel="apple-touch-icon" sizes="144x144" href="images/favicon/apple-touch-icon-144x144.png" /> <meta name="msapplication-TileImage" content="favicon-144.png" /> <meta name="msapplication-TileColor" content="#ffffff" /> <script> var baseURL = '<?php echo $baseURL; ?>'; var pageURL = '<?php echo $pageURL; ?>'; var pageName = '<?php echo $pageName; ?>'; var selectedLang = '<?php echo $selectedLang; ?>'; </script> </head> <body>
Now this does not work, and I think that I probably missed a couple of things, but I could not find a good tutorial or guide on this. Also, I'm not sure if there is anything else I need to do to start and set up a session.
Can someone help me?
Note:
This only applies to verifying that the user is already registered, since all the actual registration and verification of the user is performed on a separate login page, and for this I already have a code.
Update: Enabling error messages returns the following errors:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /homepages/21/d580042014/htdocs/index.php:2) in /homepages/21/d580042014/htdocs/includes/header.php on line 9 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /homepages/21/d580042014/htdocs/index.php:2) in /homepages/21/d580042014/htdocs/includes/header.php on line 9 array(0) { } Warning: Cannot modify header information - headers already sent by (output started at /homepages/21/d580042014/htdocs/index.php:2) in /homepages/21/d580042014/htdocs/includes/header.php on line 12 Notice: Undefined index: lang in /homepages/21/d580042014/htdocs/includes/header.php on line 18
Update:
In accordance with the comments, I have now posted everything that is currently in the header.
Thank you very much in advance.