Domanda

In PHP I set up an error message system that displays an error if the user tries to log in or register when they are already logged in. When the user clicks the button it will just be redirected to the same page with ?m=You_are_already_logged_in! at the end of the url. Then i grab this input via a GET function :

if($_GET['m'] == "You_are_already_logged_in!"){
    $_GET['m'] = preg_replace ('#[^a-z!:]#i', ' ', $_GET['m']);
    $message = $_GET['m'];
}

But when i load the page without the message value it will give a error message saying:

Notice: Undefined index: m in C:\xampp\htdocs\secret_project\index.php on line 9

Is there a way around this message? Is there a solution or should i try a different way of approach?

È stato utile?

Soluzione

You need to use isset() to check that the m key exists before attempting to read it:

if(isset($_GET['m']) && $_GET['m'] == "You_are_already_logged_in!") {...}

Altri suggerimenti

That - isset():

<?php

  if(isset($_GET['m']) AND $_GET['m'] == "You_are_already_logged_in!"){
      $_GET['m'] = preg_replace ('#[^a-z!:]#i', ' ', $_GET['m']);
      $message = $_GET['m'];
  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top