Need help with this code

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
chalee
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 6:26 pm

Need help with this code

Post by chalee »

Not sure why this code stopped working it throws no errors. Register_globals is on and the PHP version is 4.4.9 It searches a folder for images then sorts them and displays links to cycle through them Any suggestions I'm guessing is some setting and not the code cuz the code worked up until like a month or 2 ago.

<?php session_start();?>

<p align="center"><img src="topbannerfinal.jpg"></p>

<?php

session_register ("fileIndex");

$dir = "images";

$dh = opendir($dir);

while (false !== ($filename = readdir($dh))) {

$files[] = $filename;

}

if($p == ""){

$HTTP_SESSION_VARS["fileIndex"] = 0;

}

sort($files);

$img = array_slice($files, 2);

$p = $_GET['p'];

$lastFile = "last";

$nextFile = "next";

$prevLink = "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $lastFile . "\" title=\"show previous image\">&#171;</a>";

$nextLink = "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $nextFile . "\" title=\"show next image\">&#187;</a>";

?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with this code

Post by requinix »

chalee wrote:Not sure why this code stopped working it throws no errors.
Not sure why you say this code stopped working you gave no explanation.
chalee
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 6:26 pm

Re: Need help with this code

Post by chalee »

tasairis wrote:
chalee wrote:Not sure why this code stopped working it throws no errors.
Not sure why you say this code stopped working you gave no explanation.
Well the code was working fine then it just stopped working. I'm guessing that the hosting company may have upgraded to a different version of PHP. But everthing I've tried still does not get the code to work is there something different I have to do to get the globals to work?

Other than adding this to PHP.INI

register_globals = on
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with this code

Post by requinix »

chalee wrote:Well the code was working fine then it just stopped working.
And still: no explanation of what that means...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need help with this code

Post by califdon »

chalee, you must understand that we are not mind readers here. You will have to say more than "it stopped working." That means nothing. You must say exactly what happens. Does it produce any output? Do you get a blank screen? What?

And please, in the future, enclose your PHP code with [syntax=php]...[/syntax] tags in your post, so we can read the code without going blind.
chalee
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 6:26 pm

Re: Need help with this code

Post by chalee »

Sorry first time in this forum here's the code with the tags. It produces absolutely no output whatsoever. Code doesn't do a darn thing. It should and did show pictures from a directory. I'll put some comments in the code as well to help you along

Code: Select all

 
 
<?php session_start();?>
 
<p align="center"><img src="topbannerfinal.jpg"></p>
 
<?php
 
//declare a session variable for the 
session_register ("fileIndex");
 
$dir = "images";
 
$dh = opendir($dir);
 
 
while (false !== ($filename = readdir($dh))) {
 
$files[] = $filename;
 
}
 
if($p == ""){
 
$HTTP_SESSION_VARS["fileIndex"] = 0;
 
}
 
sort($files);
 
$img = array_slice($files, 2);
 
$p = $_GET['p'];
 
$lastFile = "last";
 
$nextFile = "next";
 
$prevLink = "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $lastFile . "\" title=\"show previous image\">&#171;</a>";
 
$nextLink = "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $nextFile . "\" title=\"show next image\">&#187;</a>";
 
?>
wait in looking at this it seems someone else was on my server and modified somethings here cuz there is not output for the images or links
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Need help with this code

Post by califdon »

Thank you for following our rules. And good for you, examining your own script and discovering a clue to your own problem. If you need further help, you have now learned how to present your questions and code. Good luck.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with this code

Post by requinix »

No output at all? No topbannerfinal.jpg? What if you view the HTML source of the page?
A blank page typically means a parse/syntax error. Check your code for typos or missing {}s or other small flaws.

Besides that,

Code: Select all

 
 
<?php session_start();?>
If that's how it really is in your file (and you didn't add those blank lines just in this post) then you need to get rid of everything before the opening <?php. To use session_start you can't have outputted anything yet and blank lines count.

Also, your code is a bit out of date so it's possible that has something to do with it (though it's less than likely). I'd suggest how to fix it but you only posted a small part.
chalee
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 6:26 pm

Re: Need help with this code

Post by chalee »

Thanks for all your help I did this for a friend of mine along time ago. So long I forgot what to even look for A few lines modified and it's okay. Here is the final script in case anyone want's to use it for a cheapo gallery.
Also the session start is the very first thing it's just the way it printed out here.

Code: Select all

 <?php session_start();?> 
     <p align="center"><img src="topbannerfinal.jpg">
<?php
     //register our index for the array that holds the image name
     session_register ("fileIndex");
     //declare some variables for our directory
     $dir = "images";
     $dh = opendir($dir);
     //loop through our directory gathering file names for our array
     while (false !== ($filename = readdir($dh))) {
          $files[] = $filename;
     }
    //set our index to 0 if it's our first visit to the page 
    if($p == ""){
          $HTTP_SESSION_VARS ["fileIndex"] = 0;
     }
     //sort our files and clean up our array
     sort($files);
     $img = array_slice($files, 2);
     //determine if we are moving next or back and adjust the array index accordingly to display the proper picture these get set in the link
     $p = $_GET['p'];
     $lastFile = "last";
     $nextFile = "next";
     $prevLink = "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $lastFile . "\" title=\"show previous image\">&#171;</a>";
     $nextLink = "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $nextFile . "\" title=\"show next image\">&#187;</a>";
     if($p == "next" && $HTTP_SESSION_VARS["fileIndex"] != count($img)-1){
    $HTTP_SESSION_VARS ["fileIndex"]++ ;
     }
     if($p == "last" && $HTTP_SESSION_VARS["fileIndex"] != 0 ){
    $HTTP_SESSION_VARS ["fileIndex"]-- ;
     }
// and finaly below print our links and picture
 
?>
<table  border="0" align="center">
    <tr>
         <td  align="right">
              <font size="+2"><? echo $prevLink;?></font>
         </td>
         <td align="left">
              <font size="+2"><?echo $nextLink;?></font>    
         </td>
    </tr>
    <tr>
          <td align="center" colspan="2"><? print '<img src="images/';
               echo $img[$HTTP_SESSION_VARS ["fileIndex"]];
               print '">';?>
          </td>
    </tr>
</table>
</p>
thanks for all the quick responses. Yes this code is dated I did this back in oh 2005 or so. It worked up until last week not sure how this code got modified I haven't touched it since then.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need help with this code

Post by requinix »

It could be that they disabled short_open_tags. Meaning no <? or <?= - you have to use the full <?php.

Try replacing each <? with <?php.
chalee
Forum Newbie
Posts: 5
Joined: Tue Jul 28, 2009 6:26 pm

Re: Need help with this code

Post by chalee »

The code does work now.
Post Reply