SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question how to make 1 javascript file

    Hi
    i have 4 javascript files in my page i want to make it one javascript file using php how can i make it. i want to marge the files and make one file.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could make a PHP file merge multiple files together:

    Code:
    <script type="text/javascript" src="merge.php?files=script1,script2,script3,script4"></script>
    merge.php:
    PHP Code:
    <?php
    header
    ("Content-type: text/javascript; charset: UTF-8");
    if(!isset(
    $_GET['files'])) exit;
    $files explode(','$_GET['files']);
    foreach(
    $files as $file){
        
    $file sprintf('scripts/%s.js'$file);
        if(
    file_exists($file)) readfile($file);
    }
    ?>
    Note that you shouldn't blindly output the scripts without checking that they're actually JS, otherwise PHP would output your PHP files if someone wished. Instead, force it to have a JS extention like above.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot shoorace's Avatar
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good way of merging files using file handling functions

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're going to do this, consider sending some cache friendly headers. Otherwise the browser will keep requesting and downloading the javascript file on every page load. I'm guessing your combining these files into one file in an attempt for browser speed optimization. Without sending cache friendly headers, you're making it much worse unless the page which uses these javascripts will only be viewed a single time.

    eg
    PHP Code:
    // cache for 1 day
    $time_to_live 86400;
    header(sprintf('Expires: %s GMT'gmdate("D, d M Y H:i:s"time() + $time_to_live)));
    header('Cache-Control: public, max-age=' $time_to_live); 

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    And I guess you want to do it dynamically (using PHP) because the js files aren't always the same? Otherwise, you might just create one js file by hand

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    [code]
    PHP Code:
        $file sprintf('scripts/%s.js'$file); 
    Theres a little known but very nasty attack this is vulnerable to.
    Passing a null byte will cause filesystem functions to ignore the rest of the string after that point. This can be used to allow them to specify the file extensions(or omit it).

    script.php?files=foo.php%00
    script.php?files=../../../etc/passwd%00


    Ironically, magic_quotes_gpc saves the day here because it escapes null bytes. But not all of us keep that setting turned on anymore


Tags for this Thread

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
  •