Reputation: 167
Notice: Undefined index: message in C:\xampp\htdocs\mywebsite\Untitled9.php on line 27
<?php session_start();
if ($_GET['message'] == "success" && $_SESSION['revisit'] == "0") {
$_SESSION['revisit'] = "1"; ?>
<script type="text/javascript">window.alert('Message successfully posted.');</script>
<?php } ?>
Line 27 is....
if ($_GET['message'] == "success" && $_SESSION['revisit'] == "0") {
How do i fix this?
Upvotes: 2
Views: 22772
Reputation: 9121
It's a notice
and not a fatal error
which indicates you can either surpress this error by changing your error_reporting
settings, adding a @
in front of the line of code, or doing a check if the variable exists first.
if(isset($_GET['message']))
The last might be the most elegant, because it prevents the error instead of surpressing it. However surpressing isn't a problem here since it doesn't affect your code.
Upvotes: 2
Reputation: 2431
try this:
if (isset($_GET['message']) && $_GET['message'] == "success" && $_SESSION['revisit'] == "0") {
Upvotes: 0
Reputation: 287915
Do not assume the message
parameter has been set:
if (isset($_GET['message']) && $_GET['message'] == 'success' && ...) {
...
}
Alternatively, you can provide a default value:
$message = isset($_GET['message']) ? $_GET['message'] : 'no_message';
if ($message == 'success' && ..) {...
Upvotes: 0
Reputation: 254956
You need to check if there is an item with key message
in array $_GET
with
isset($_GET['message'])
And the same rule is applied to the second comparison of session item. So the complete code could look like
if (isset($_GET['message']) && $_GET['message'] == "success" && isset($_SESSION['revisit']) && $_SESSION['revisit'] == "0") {
And even if anyone will propose you to use @
instead - don't believe them. It is evil. Follow the way I proposed.
Upvotes: 5