SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable wont work in echo

    PHP Code:
    // Directory containing the images - need the trailing slash /
    $read 'site-list/';
      
    // Open the directory and create an array of the images
    chdir($read);
    $dirArray glob("*.jpg"); 

    // ... cut out unnecessary
       
    echo '<li style="display: inline;">
    <a href="http://www.'
    .$dirArray1[$i].'" target="_self">
    <img border="1" src="'
    $read'.$dirArray[$i].'" alt="www.'.$dirArray1[$i].'" /></a>
    <br />
    </li>'; 
    How come I cant get $read to work in this echo?
    I tried several variations but cant get it to echo site-list/

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is it just a typo or is it actually wrong?

    $read (in echo) should have periods around to concatenate.


    .$read.

    and close the quote after $dirarray[$i] NOT $read
    ---------------------------
    Errors = Improved Programming.
    My Site

  3. #3
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<li style="display: inline;">
    <a href="http://www.'
    .$dirArray1[$i].'" target="_self">
    <img border="1" src=".$read.$dirArray[$i]." alt="www.'
    .$dirArray1[$i].'" /></a>
    </li>'

    having trouble, that aint working

    Im new to php

    man i need a php coder so i can read it better

  4. #4
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I tried PSPad, that makes the colors readable so i know if its right

    PHP Code:
    echo '<li style="display: inline;">
    <a href="http://www.'
    .$dirArray1[$i].'" target="_self">
    <img border="1" src='
    .$read.$dirArray[$i].' alt="www.'.$dirArray1[$i].'" /></a>
    </li>'


  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure the read has something in it at the point you echo it?

    how about some debugging?

    right above this echo statement add another line

    var_dump($read);

    and then post the result.
    ---------------------------
    Errors = Improved Programming.
    My Site

  6. #6
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah i just shortened it because i couldnt figure out what was wrong, I fixed my editor to make variables bold so i can read them better.

    PHP Code:
    <?php

    // Directory containing the images - need the trailing slash /
    $read 'site-list/';
      
    // Open the directory and create an array of the images
    chdir($read);
    $dirArray glob("*.jpg"); 
     
    // Count the number of images
    $indexCount count($dirArray);
      
    // Setup the table layout
    echo '<ul>'
    for (
    $i=0$i<$indexCount$i++)
    {


    // Remove the .jpg extension from the image name for use in the image alt.
    $altTag[$i] = substr($dirArray[$i], 0, -4);

    // Link to the image URL - extension

     
    $dirArray1[$i]=substr($dirArray[$i], 0, -4);
       
    echo 
    '<li style="display: inline;">
    <a href="http://www.'
    .$dirArray1[$i].'" target="_self">
    <img border="1" src='
    .$read.$dirArray[$i].' alt="www.'.$dirArray1[$i].'" /></a>
    </li>'


    }

    echo 
    '</ul>';

    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this code...it's a bit cleaner and uses a better security model.
    PHP Code:
        /*
         * Define the host domain
         */
        
    define('HOST_DOMAIN''http://www.' $_SERVER['HTTP_HOST']);

        
    /*
         * Define the document root
         */
        
    define('DOC_ROOT'$_SERVER['DOCUMENT_ROOT']);

        
    /*
         * Define the webserver images directory
         * (1) MUST be relative to document root
         * (2) MUST have leading and trailing slashes
         */
        
    define('READ_DIRECTORY_WS''/site-list/');

        
    /*
         * Define the filesystem directory
         */
        
    define('READ_DIRECTORY_FS'DOC_ROOT READ_DIRECTORY_WS);

        
    /*
         * Define the end linefeed, optional
         */
        
    define('LF'"\n");

        
    /*
         * Use the glob function to loop through the JPG images
         */
        
    foreach( glob(READ_DIRECTORY_FS "*.jpg") as $filename )
        {
         
    /*
            *    Get the image basename
            */
            
    $imageName basename($filename);

         
    /*
            *    Get the actual image name minus the jpg extension
            */
            
    $imageActualName basename($filename'.jpg');

         
    /*
            * Get the height/width string for image
            */
            
    $attributes getimagesize($filename);

         
    /*
            * Echo the link and image source
            */
            
    echo '<a href="' HOST_DOMAIN READ_DIRECTORY_WS $imageName .'">' LF;
            echo 
    '<img border="1" src="' READ_DIRECTORY_WS $imageName .'" ' $attributes[3] . ' alt="'.$imageActualName.'" /></a><br />' LF;
        } 
    # end foreach 
    Bobby
    Last edited by Chemo; Jun 16, 2006 at 06:08. Reason: Added code to get image actual name minus JPG extension

  8. #8
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont understand how this is more secure though.

    But I do find it a lot easier to read with // comments =)

    PHP Code:
    <?php

    //Define the host domain
    define('HOST_DOMAIN''http://www.' $_SERVER['HTTP_HOST']);

    //Define the document root
    define('DOC_ROOT'$_SERVER['DOCUMENT_ROOT']);

    // Define the webserver images directory
    // (1) MUST be relative to document root
    // (2) MUST have leading and trailing slashes
    define('READ_DIRECTORY_WS''/site-list/');

    //Define the filesystem directory
    define('READ_DIRECTORY_FS'DOC_ROOT READ_DIRECTORY_WS);

    //Define the end linefeed, optional
    define('LF'"\n");

    //Use the glob function to loop through the JPG images

    foreach( glob(READ_DIRECTORY_FS "*.jpg") as $filename )
        {
        
    //Get the image basename
        
    $imageName basename($filename);

        
    //Get the actual image name minus the jpg extension
        
    $imageActualName basename($filename'.jpg');

        
    //Get the height/width string for image
        
    $attributes getimagesize($filename);

        
    //Echo the link and image source
        
    echo '<a href="' HOST_DOMAIN READ_DIRECTORY_WS $imageName .'">' LF;
        echo 
    '<img border="1" src="' READ_DIRECTORY_WS $imageName .'" ' $attributes[3] . ' alt="'.$imageActualName.'" /></a><br />' LF;
        } 
    # end foreach 
    ?>

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lostseed
    I dont understand how this is more secure though.

    But I do find it a lot easier to read with // comments =)
    Fair enough...let's start the disussion and see if you can answer the question yourself.

    First question and hint: Do you see any difference between running the original code with variables as file paths versus the revised version on a register globals enabled server?

    Bobby

  10. #10
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see where you are coming from Chemo. But as i look at that code i dont see it wrong, PROVIDING he is initilizing everything which he is using. So, doesn't matter what their values were. Those are being overwritten.

    But, your point is good anyways.
    ---------------------------
    Errors = Improved Programming.
    My Site

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @jaswinder => You take the fun out of learning...you're suppose to let him answer

    In all seriousness, I'm used to writing applications and scripts that are deployed across many versions of PHP (CGI versus SAPI, register globals enabled versus disabled, 4.3.X verus 5.X, etc). As a result I instinctively don't trust variables to open, read, or output directories EVEN if they are initialized immediately before use. If it has to do with anything filesystem or command (exec, eval, etc) I define the string or path explicitly.

    Admittingly, I code in paranoid mode...but it keeps me on my toes

    Bobby
    Last edited by Chemo; Jun 16, 2006 at 09:30. Reason: edit for humor clarification

  12. #12
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah well, then you shoulda mention in the SP secret writiting which doesn't exist yet
    ---------------------------
    Errors = Improved Programming.
    My Site

  13. #13
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean, but is it really necessary?
    I dont see how this could even allow any vulnerabilities, besides the fact that it does look more secure by using SERVER_VARS, but to me thats only the appearance. I am not dont know much about PHP Security and really have no clue what difference this makes besides the automation.

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lostseed
    I see what you mean, but is it really necessary?
    For me it is absolutely necessary...because I release a lot of scripts that are GPL and used by various communities (especially osCommerce). Not only is functionality important but security as well since the last thing I want is a store owner to realize a security hole in my code resulted in the compromise of their site.

    Your mileage may vary...

    Bobby

  15. #15
    SitePoint Zealot
    Join Date
    May 2006
    Location
    Phoenix, AZ
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lostseed
    PHP Code:
    // Directory containing the images - need the trailing slash /
    $read 'site-list/';
      
    // Open the directory and create an array of the images
    chdir($read);
    $dirArray glob("*.jpg"); 

    // ... cut out unnecessary
       
    echo '<li style="display: inline;">
    <a href="http://www.'
    .$dirArray1[$i].'" target="_self">
    <img border="1" src="'
    $read'.$dirArray[$i].'" alt="www.'.$dirArray1[$i].'" /></a>
    <br />
    </li>'; 
    How come I cant get $read to work in this echo?
    I tried several variations but cant get it to echo site-list/



    You can't use single quotes to do inline vars.. ' double quotes "

    either change the dubs to sing's and escape your array values with curly braces for inline code..

    here..

    PHP Code:
    // Directory containing the images - need the trailing slash /
    $read 'site-list/';
      
    // Open the directory and create an array of the images
    chdir($read);
    $dirArray glob("*.jpg"); 

    // ... cut out unnecessary
       
    echo "<li style='display: inline;'>
    <a href='http://www.
    {$dirArray1[$i]}' target='_self'>
    <img border='1' src='
    $read{$dirArray[$i]}' alt='www.{$dirArray1[$i]}' /></a>
    <br />
    </li>'; 
    fyi... border='1' is depreciated as of xhtml 1.0...try using <img style='border:1px solid blue'>
    Kreative Development
    Infinite Possibilities

  16. #16
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    briancastellanos, if you look at it, he is using single quotes, BUT concatenating the variables with it. He is NOT using variables INSIDE the quotes.
    Last edited by jaswinder_rana; Jun 17, 2006 at 09:50.
    ---------------------------
    Errors = Improved Programming.
    My Site

  17. #17
    Non-Member lostseed's Avatar
    Join Date
    Sep 2005
    Location
    Zurconion Soon!
    Posts
    1,549
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol Jaswinder, im not familiar with the word concatinating :P

  18. #18
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just wanted to see whether you know it or not
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •