Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hints please on error

Status
Not open for further replies.

jen0dorf

IS-IT--Management
Apr 15, 2008
104
0
0
GB
Hi
at an error appears that says:-
Undefined variable: admin in e:\domains\p\puxton.co.uk\user\htdocs\index.php on line 38
the line says: if($admin == true)
print("<div class=\"adminlinkfront\"><a href=\"edit.php?item=news&id=1\">edit news headline</a></div>");

which I take to mean that if the variable admin is true print the next bit on the screen. The variable is set at the top:

<?php
session_start();
if(isset($_SESSION['loggedin'])){
if($_SESSION['loggedin'] == TRUE)
$admin = TRUE;
}
include("database.php");
$db = new Database();
$text = $db->query("SELECT content FROM news WHERE newsid=1",'cell');

?>

This bit has lost me because an enduser would not belogged in??? and I thought it said if the session is logged in then the variable admin is true? so why isit an undeclared variable??

Any help appreciated

thanks

Ian
 
because if the test fails then $admin is not set. add this after the session_start()
Code:
$admin = false;

 
What can I say! like all questions simple if you know the answer. At leastI have the satisfaction of knowing I was on the right lines.

I will become fluent in "phpese"

many thanks

Ian
 
no worries.

you'll find as well that the error you reported was actually a notice. typically for deployed solutions you will suppress the reporting of notices if not all error messages. you control this through php.ini (and other means)

 
I want to add one more observation
Code:
if(isset($_SESSION['loggedin']))
{
  if($_SESSION['loggedin'] == TRUE)
     $admin = TRUE; 
}
As you can see { } pair represents an scope, and so anything outside that scope is not declared outside, if you want to use that variable out of scope you had to declare it before as jpadie sayed.
Since you used { } scope you dont have to use global when you declare the variable. But if you use a function instead, you need to use the global keyword.

Code:
//this works
$admin = false;
if(isset($_SESSION['loggedin']))
{
  if($_SESSION['loggedin'] == TRUE)
     $admin = TRUE; 
}


//this wont work
$admin = false;
function check_login()
{
  if(isset($_SESSION['loggedin']))
  {
   if($_SESSION['loggedin'] == TRUE)
      $admin = TRUE; 
  }
}


//this will work now
$admin = false;
function check_login()
{
  [b]global[/b] $admin;
  if(isset($_SESSION['loggedin']))
  {
   if($_SESSION['loggedin'] == TRUE)
      $admin = TRUE; 
  }
}

Just in case you needed an detailed explanation.

________
George, M
Searches(faq333-4906),Carts(faq333-4911)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top