Jump to content

undefined variable message


spdwrench

Recommended Posts

when I turned errors on in one of my php pages I got this error..

 

but the page still works fine..

 

I never had to declare a variable in php... why is it telling me I have an undefined?

 

I thought they were defined when you define them?

 

just wierd

 

Paul

Link to comment
https://forums.phpfreaks.com/topic/66924-undefined-variable-message/
Share on other sites

At login set a session or cookie

 

 

in this case you mentioned cookies

 

setcookie("nameofcookie", "valueofcookie", time()+3600, "/", "yoursite.com", 1);

 

name of the cookie maybe be "users"

 

value could be under a field like "auth_lvl" ranging from 1-3 1 being a super admin and then down the line

 

do this in secure areas

 

if (!isset($_COOKIE['users'])) {

  header(); // send them somewhere else

  exit;

}

 

then to check auth_lvl

 

if ($_COOKIE['users'] != 1) {

  die("Unauthorized");

}

 

but $_SESSIONS are bettter to use

As you said earlier, the page still works - so in that sense it does not matter. All it tells you is that you have used some bad coding practice... take this example:

 

script.php

<?php
error_reporting(E_ALL);

$id = $_GET['id'];

echo $id;
?>

 

If I call the script.php without the ID get argument PHP will throw a notice saying that I have an undefined index... all this means is that $_GET['id'] does not exist even though I'm using it. The end result is of course that $id will hold the value '' and nothing will be printed to the screen... fine. But it would be good practice to test for the existence of $_GET['id'] before using it.

 

if($_GET['id'])  {

    $id = $_GET['id'];

}

 

 

I know it's not the exact same thing, but it a similar case. If you wanna know exactly why it says undefined (which is only a part of the error - what is the rest?) you will have to post the whole error message :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.