Вопрос

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?

Это было полезно?

Решение

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!") {...}

Другие советы

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'];
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top