Very very weird problem..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Very very weird problem..

Post by Elfstone »

Hi, here's the situation..I've written a simple(at the moment) script that will create user accounts, and allow users to log in. As part of the layout I want to add a script that will give a logged in user links, or if not logged in a login box. The way the layout is set up, is I have two files which contain the layout that are included using php's include function on every page. The following is the code I have to check to see if the user is logged in and print out the info I want:

Code: Select all

<?php 
if (isset($eslogin)) &#123;
$luid = $eslogin&#1111;'user'];
 while (list ($name, $value) = each ($eslogin)) &#123;
    if ($name == user) &#123;
      $liuid = $value;
      global $liuid;
    &#125;
  &#125;
&#125;
if (isset($liuid)) &#123;
echo "<center>";
echo "<font face=verdana size=1>Welcome, $liuid</font><br>";
echo "<a href='http://elf.pp-network.com/logout.php'>Logout</a>";
echo "</center>";
&#125; else &#123;
    echo "<center>";
    echo "<form name='register' action='execlogin.php' method='GET'>";
    echo "<input type='text' size='15' value='Username' name='uid'><br>";
    echo "<input type='text' size='15' value='Password' name='pass'><br>";
    echo "<input type='submit' value='Login'>";
    echo "</form>";
    echo "</center>";
  &#125;
?>
This is where it gets weird. If you access the file directly(i.e. viewing the actual file the code is in in your browser), it works without a hitch. However, if you access the file on a page it is being included on, it will echo out the form to login, even though it should be echoing out the Welcome message as it does if the file is accessed directly. I can't think of any reason for this to be happening and would appreciate any help. :)
Wandrer
Forum Newbie
Posts: 21
Joined: Thu Jun 06, 2002 8:43 am

Post by Wandrer »

When you include it from another page, is '$eslogin' set ?
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

Apparantly not...when I throw an else statement in there and go to a page on which it is included it returns what I ask it to if it is not set. Though, because the value is stored in a cookie this seemingly shouldn't be.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try putting,

Code: Select all

echo '<pre>';
print_r($_COOKIE);
echo '</pre>';
at the top of the script (change $_COOKIE to $HTTP_COOKIE_VARS if PHP < v.4.1). To see what cookies are set.

The problem could arise if you set a cookie and then immediately try to access it,
php manual - setcookie() wrote:Cookies will not become visible until the next loading of a page that the cookie should be visible for.
This means that the page would have to be refreshed before the cookie could be used. Not necessarily your problem, just a suggestion.

Mac
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

I used the code you pasted and all it returns is Array ().

The cookie is already set and being accessed on a different page from when it was set, so that isn't the problem. :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If all it returns is Array(), then the cookie isn't set because the $_COOKIE array is empty.

Mac
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

What could possibly be the problem then? Why is it set if I access the file directly and not set when the file is included?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What does the code of the page it's included in look like?

Mac
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

Well, it doesn't work on any of the pages it is included on, but here is everything inside my index page.

Code: Select all

<?php include('http://elf.pp-network.com/header.php'); ?>
<?php include ('http://elf.pp-network.com/nphp/news.txt'); ?>
<?
if(file_exists("/home/tan-site/public_html/elf/counter.txt")) &#123;
  $count_file = fopen("/home/tan-site/public_html/elf/counter.txt", "r");
  $viscount_new = fgets($count_file, 255);
  $viscount_new++;
  fclose($count_file);
  print("$viscount_new people have visited ElfSite.");
  $count_file = fopen("/home/tan-site/public_html/elf/counter.txt", "w");
  fputs($count_file, $viscount_new);
  fclose($count_file);
&#125; else &#123;
$count_filenew = fopen("/home/tan-site/public_html/elf/counter.txt", "w");
fputs($count_filenew, "1");
print("1 person has visited ElfSite.");
fclose($count_filenew);
&#125;
?>
<?php include('http://elf.pp-network.com/footer.php'); ?>
Elfstone
Forum Newbie
Posts: 21
Joined: Tue Jun 11, 2002 2:25 am
Location: NJ, USA

Post by Elfstone »

I've found the problem...in my include statement I was using a URL to include the files. I changed it to use the path instead and the script works perfectly.
Post Reply