Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Super 3 posted:

php_value max_execution_time X

Is also another good one to throw in there. Those three are my standard .htaccess file upload fix.
I don't put these into .htaccess because .htaccess isn't parsed if you're running a CLI script. I always either set them explicitly in my bootstrap, or in php.ini if that particular var doesn't allow it.

Super 3 posted:

I was wondering if anyone could point me to a php progress bar script of some kind. I need it to be able to display sales for about 12 different entities on one page. I've been digging around quite a few sites to no avail.
I'm not sure whether you're after a progress bar or some charting tools. Which is it?

Adbot
ADBOT LOVES YOU

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

admiraldennis posted:

I agree, though delete() isn't even a built-in function...
Yes it is: http://jp2.php.net/manual/en/function.delete.php

I tend to shy away from reusing built-in functions because it makes the syntax highlighting in my editor look odd. All built-in functions are highlighted in blue, and all others are in grey, so it looks kinda strange to see one of my functions in blue.

This usually isn't a problem as I use a camelCase naming convention, so overlap is very rare.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
Or just use exec() or something to run diff directly (assuming your host is running Linux).

diff is a pretty clever program, I wouldn't want to be re-inventing that unless I absolutely needed to.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
Are you using double back slashes? If not, then the actual string will be "cd c:pathWithSVN". Try using single quotes.

Edit: Also, the "cd blah" thing is never going to work, the scope is limited to that exec call only. Use chdir as brae suggests.

Also, use escapeshellarg() to ensure that your command line parameters get passed correctly.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
I'm with Bonus, sanitize when you need to. If you sanitize before you write to the DB it means that:

- You can present the user with exactly what they wrote if they go back to edit it. We have a comment system that you can enter HTML into, and we use HTMLtidy to clean it and sanitize it. The guy who implemented the system made it so that the data was always cleaned before writing, and so we get users confused as to why their post has tags inserted into it that they didn't write themselves.

- As a coder you can get a false sense of security by assuming that all DB data is clean.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

nbv4 posted:

I have this one class which is getting so huge, it's almost 2000 lines. I want to split it up into smaller text files to make editing easier, but I'm having trouble doing so. Apparently you can't just do:

php:
<?

class foo extends lol
{
     include "text_file_with_methods.php";

     function blah()
     {
       ...
?>
nor can you have a class extending from multiple classes. What can I do here?

This is where C#'s "partial class" definitions feature would come in handy. But we don't have that, so here's what I do with my 18000 line class that contains all the DB schema upgrades we've made since version 1:


php:
<?
class DBUpgraderBase {
  function upgrade_version_1() {
     ...
  }
  function upgrade_version_2() {
     ...
  }
  ...
  function upgrade_version_99() {
     ...
  }
}
?>
and in another file
php:
<?
class DBUpgraderBase1 extends DBUpgraderBase {
  function upgrade_version_100() {
     ...
  }
  ...
  function upgrade_version_199() {
     ...
  }
}
?>
and so on, until finally we get to:
php:
<?
class DBUpgrader extends DBUpgraderBase6 {
  function upgrade_version_700() {
     ...
  }
}
?>
That way we just instantiate "DBUpgrader" and it will automatically include all the functions from the parent classes.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Safety Shaun posted:

I'm looking to stop image leeching on my server by using a fancypants URL hiding script.
You have a couple of solutions at hand.

The easiest is just to check the Referrer header with mod_rewrite and redirect the user to a 404 page if it's not your site. However some browsers/proxy servers don't send the Referrer header 100% of the time so its presence or absence alone is not enough - the best and easiest thing to do here is to only let the image be displayed if the Referrer header is present and points at your site, OR isn't present at all. I believe this is what Waffleimages does.

A more accurate solution would be to set a cookie when someone visits your site, and then check for the presence of that cookie when serving the images. Anyone leeching won't have the cookie and thus won't get the image. This can probably be done with mod_rewrite (but I don't know for sure). If not then use a PHP script to check for it. If the cookie is present, then just do:
php:
<?
header('Content-type: image/jpeg');
$path_to_file = $_GET['file_name'];
... //validate and sanitize $path_to_file here
http_send_file($path_to_file);
exit;
?>

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Safety Shaun posted:

Ahh yes of course, thank you.

If nobody chimes in with the way I originally planned I'll do that.
Isn't this the same question that you asked that 3 people answered on the last page? http://forums.somethingawful.com/showthread.php?threadid=2802621&userid=0&perpage=40&pagenumber=7#post342302162

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
You cannot "break" out of an if statement. break is for loops and switch statements.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Kaluza-Klein posted:

Why does this only work for the "The " value? If the string starts with "A " or "An " it doesn't catch it.
It works fine for me. Except the function isn't actually returning the modified string.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Lankiveil posted:

I'm looking at implementing my own RSS feed generator, which will work off of a series of existing tables in my database. I had planned to simply write a PHP script that output the required XML straight to the client, but most of the tutorials that I've seen on the web do it by generating a flat text file at a regular interval and having that serve as the feed.

I had hoped to be able to do customisable feeds (ie: so the user can filter out particular contributors or topics and get a feed containing only that), but if I'm to generate flat files that will mean I'll need to do hundreds of them to cover every possibility. Is there some compelling reason why I can't just take a querystring (rewritten with mod_rewrite, of course) and spit the output directly to the user based upon the input parameters?
Unfortunately the RSS standard does not specify a concrete way to say how often a RSS client will poll the server to retrieve the XML file. It might hit it every few hours, every 15 minutes, or at worst every minute or two. So the server needs to be able handle this constant polling, multiplied by the expected number of RSS clients. If the cost of generating an XML feed is running a PHP script and probing the database, then that can be expensive and inefficient when the number of clients is high and the content is infrequently updated. So a common optimization is to use a cronjob to write the feed to a flat file and serve that instead. That way it's a fixed cost for the server to generate it, and a much smaller cost for the server to serve it. In addition, the client can also use caching techniques like Etags to avoid retrieving the whole feed on every poll.

This optimization works very well for blogs and the like, which are updated probably a couple of times a day at most, and have feeds that are not customized for any particular reader. But it breaks down when feeds need to be customized, because customization goes against the grain of cachability. The most custom something is, the less effective caching techniques will work.

The easiest way to implement a customized feed is just to generate it from scratch every time and return it, i.e. don't cache it to a flat file on the disc. But this won't scale well. So you could cache each custom feed in a file, but then that'll mean you need a file for each customized feed. This is more complex to manage, needs more disc space, and you'll need a cronjob or something to clean up stale feeds.

Depending on the number of clients I was expecting to see, I might try caching the feeds with memcached or something. The way it'd work is that the PHP script would generate a unique key based on the custom feed parameters, and check to see if that feed was present in memcache. If it is, return it. If not, generate it, stick it in memcache, and then return it. This way you can cache a very large number of feeds, and not have to worry about cleanup of the flat files.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

drcru posted:

What's the best or easiest way to add language support? By language support I mean we can display an error message in English or maybe French rather than just English.
For static or printf()-style strings, use gettext. For user-editable stuff, you have to roll your own.

Pick the language based on the HTTP request "Accept-Language" header, but always allow the user to override it by setting a cookie or something.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

mcbuttbutt posted:

It works for smaller files, but larger files (>50 megs) are always incomplete. Any idea why this is happening? Thanks!
You're probably hitting the PHP 30 second execution time limit.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
Try adding "while(@ob_end_clean());" before you start outputting data.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

iamstinky posted:

I am trying to implement a reversible sort a 2d array by an arbitrary key in the second level function.

Is there a better way to do this if I need to be able to sort on class or person_id etc? $array size won't ever be more than a couple thousand elements.
You mean like this?
php:
<?
    /**
     * sortMultiArray
     *
     * Input: a 2-dimensional array, where the all the 2nd-dimension elements have a common field name (e.g. 'name')
     * It will sort the array based on a specific field name.
     * E.g. if the array contains many arrays like ('name'=>..., 'address'=>...)
     * then calling sortMultiArray($arr, 'name') will return the array sorted by name.
     * The key of the main array is not retained.
     *
     * @param array $arr - Array to sort
     * @param string $field - field name to sort on
     * @param boolean $reverse - Reverse sort order. false (default) - ascending, true - descending
     * @param string $function - name of the sorting function. (strnatcasecmp is default). To pass in a static class
     *      method (a non-static class method is not permitted), use the form 'StaticClass::methodname'.
     * @return array - the sorted array.
     */
    function sortMultiArray($arr, $field, $reverse=false, $function=null) {
        $rev = '';
        if($reverse) {
            $rev = '-';
        }
        if(!$function) {
            $function = 'strnatcasecmp';
        }
        $function_def = 'return '.$rev.$function.'($a[\''.$field.'\'],$b[\''.$field.'\']);';
        $function = create_function('&$a,&$b',$function_def);
        usort($arr, $function);
        return $arr;
    }
?>

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

illamint posted:

How do you guys deal with really long SQL queries in your PHP code?

do this:
php:
<?
$query = "SELECT ...
          FROM ...
          ORDER BY ...
          ";
?>
It is sooooo much nicer than string concatenation.

Edit: Also, a pro-tip: When putting comma-separated things onto separate lines, put the commas at the start and line them up. That way you can easily see when a comma is missing, which is harder to do when commas go at the end. Plus, it's easier to add new lines to the end and not miss putting the comma in. Example:

php:
<?
$sql = "CREATE TABLE foo
            ( bar integer
            , baz string
            , wiz float
            , waz integer
            , PRIMARY KEY (bar)
            )
        ";
?>

minato fucked around with this message at 18:40 on Jun 19, 2008

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
Use gettext.

Looks like this:
php:
<?
$some_string = _("Welcome to our site");?>
In a Smarty template (which I highly recommend you use) with the gettext extension, it looks like this:
code:
<h1>{t}Welcome to our site{/t}</h2>
If the translation isn't available or not found, the English version is used.

You'll need to write a script to extract the strings from your code. The xgettext tool already supports extracting them from PHP, but you may need something custom for Smarty.

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Munkeymon posted:

Anyone know what it means when mb_detect_encoding returns an empty string? Clearly it's failing at detecting the encoding, but the drat thing has to have some encoding (and it should be UTF-8).
UTF8 looks identical to ASCII if all the character codes are below 127.

I've found it really hard to reliably detect an encoding. And it doesn't help that some people seem to equate Windows-2352 with ISO-8859-1.

Adbot
ADBOT LOVES YOU

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender
I use a complicated system that involves throwing the text out to this little application that's used in Firefox's test harness. The app's job is to help test the encoding detection of Firefox, you just send it some text via stdin and it tells you what it thinks the encoding is. But apart from requiring the app to shell out to an external program, it requires building Firefox from source with all the test stuff enabled which is a horrible process. Thankfully I don't have to do it very often - just when trying to parse NNTP/mail posts from Outlook which breaks standards by not including the encoding.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply