AnonymousProgrammer Posted March 29, 2017 Share Posted March 29, 2017 <?php /* Displays user information and some useful messages */ session_start(); ?> <!DOCTYPE HTML> <head> <title>CashBallz</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]--> <link rel="stylesheet" href="assets/css/main.css" /> <!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]--> <!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]--> </head> <body class="landing"> <div id="page-wrapper"> <!-- Header --> <header id="header"> <h1 id="logo"><a href="index.php">CashBallz</a></h1> <nav id="nav"> <ul> <li><a href="index.php">Home</a></li> <li> <a href="#">Play</a> <ul> <li><a href="left-sidebar.php">Server 1</a></li> <li><a href="right-sidebar.php">Server 2</a></li> <li><a href="no-sidebar.php">Server 3</a></li> </ul> </li> <li><a href="elements.php">Paypal</a></li> <li><a href="loginsystem/loginpage.php" class="button special">Sign Up/Login</a></li> </ul> </nav> <br> <p style="float:right; font-size: 30px" > <?php if ($_SESSION['logged_in'] = true){ $first_name = $_SESSION['first_name']; $last_name = $_SESSION['last_name']; } ?> Welcome <?= $first_name.' '.$last_name ?></title> ?> </p> </header> I am getting the error: Undefined index: first_name in (my file name) on line 41 and, Undefined index: last_name in (my file name) on line 42 How do i fix it? I am new to php. Thank you! Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 29, 2017 Share Posted March 29, 2017 (edited) You need to learn the difference between comparison operators and assignment operators as well as how to use the code tags when you post. Edited March 29, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
AnonymousProgrammer Posted March 29, 2017 Author Share Posted March 29, 2017 You need to learn the difference between comparison operators and assignment operators as well as how to use the code tags when you post. So how do i fix it? Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 29, 2017 Share Posted March 29, 2017 I just told you. You obviously took no time at all to look into what I posted. Maybe someone else will spoon feed you the answer. On help forums you are expected to put some effort in. 1 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 You don't seem to understand the difference between a variable assignment (“=”) and an equality check (“==”). Your if statement uses an assignment instead of a check, and the assignment is always true. So you always try to access the first and last name, even if the user isn't logged in. On top of that, the “Welcome” message is outside of the statement, so it also happens in any case. Fix that: <?php // any HTML input *must* be HTML-escaped to prevent the user from injecting malicious JavaScript code function html_escape($raw_input, $encoding) { return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding); } session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <?php if (isset($_SESSION['logged_in'])): // use isset() checks instead of simply accessing data that may not exist; this prevents warnings ?> Welcome <?= html_escape($_SESSION['first_name'], 'UTF-8') ?> <?= html_escape($_SESSION['last_name'], 'UTF-8') ?>! <?php else: ?> Welcome guest! <?php endif; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
AnonymousProgrammer Posted March 29, 2017 Author Share Posted March 29, 2017 I just told you. You obviously took no time at all to look into what I posted. Maybe someone else will spoon feed you the answer. On help forums you are expected to put some effort in. Oh sorry, i didn't see the link t the bottom of your answer, I thought that was just your account description. I'll look into it. Hopefully someone does spoon-feed me the answer though. Quote Link to comment Share on other sites More sharing options...
AnonymousProgrammer Posted March 29, 2017 Author Share Posted March 29, 2017 You don't seem to understand the difference between a variable assignment (“=”) and an equality check (“==”). Your if statement uses an assignment instead of a check, and the assignment is always true. So you always try to access the first and last name, even if the user isn't logged in. On top of that, the “Welcome” message is outside of the statement, so it also happens in any case. Fix that: <?php // any HTML input *must* be HTML-escaped to prevent the user from injecting malicious JavaScript code function html_escape($raw_input, $encoding) { return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding); } session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <?php if (isset($_SESSION['logged_in'])): // use isset() checks instead of simply accessing data that may not exist; this prevents warnings ?> Welcome <?= html_escape($_SESSION['first_name'], 'UTF-8') ?> <?= html_escape($_SESSION['last_name'], 'UTF-8') ?>! <?php else: ?> Welcome guest! <?php endif; ?> </body> </html> I love you. Thanks so much. Quote Link to comment Share on other sites More sharing options...
AnonymousProgrammer Posted March 29, 2017 Author Share Posted March 29, 2017 You don't seem to understand the difference between a variable assignment (“=”) and an equality check (“==”). Your if statement uses an assignment instead of a check, and the assignment is always true. So you always try to access the first and last name, even if the user isn't logged in. On top of that, the “Welcome” message is outside of the statement, so it also happens in any case. Fix that: <?php // any HTML input *must* be HTML-escaped to prevent the user from injecting malicious JavaScript code function html_escape($raw_input, $encoding) { return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding); } session_start(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> </head> <body> <?php if (isset($_SESSION['logged_in'])): // use isset() checks instead of simply accessing data that may not exist; this prevents warnings ?> Welcome <?= html_escape($_SESSION['first_name'], 'UTF-8') ?> <?= html_escape($_SESSION['last_name'], 'UTF-8') ?>! <?php else: ?> Welcome guest! <?php endif; ?> </body> </html> Actually never mind, i'm really sorry but i'm still getting the same error. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 Then your log-in procedure is fudged up. Show the code. Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 29, 2017 Share Posted March 29, 2017 What is the exact code you are using now? (Use the code tags) Quote Link to comment Share on other sites More sharing options...
AnonymousProgrammer Posted March 29, 2017 Author Share Posted March 29, 2017 Then your log-in procedure is fudged up. Show the code. https://drive.google.com/open?id=0B-5Ul-ssAcQNMkRLOGIwOWxlVHc here is the folder Quote Link to comment Share on other sites More sharing options...
Kosonome Posted March 29, 2017 Share Posted March 29, 2017 Undefined index: first_name in (my file name) on line 41Undefined index: last_name in (my file name) on line 42 "Undefined index" errors occurs when you trying accessing something that does not exists. When you first put if ($_SESSION['logged_in'] = true) Inside $_SESSION['logged_in'] is now 'true'. Since it's a session, it stays like that until you clear your cookies/session in your browser. Or session_destroy(). Because it's true, and you fixed to '==', it's entering this condition now, but inside $_SESSION['first_name'] there is nothing. 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.