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
Xenos
Jun 17, 2005

I've been writing PHP for my job for over a year now, and we don't use a framework. I'm going to recommend two tools that make using PHP less messy.

The first is Smarty, a templating language for PHP. It's overkill for small projects, but if you're working on something bigger than one page, it can really help you avoid poo poo like "echo '<html><head>...'". If you've ever hated mixing a ton of PHP code in with gigantic blocks of HTML, you'll probably want to try Smarty.

The second tool is MDB2. It's a database abstraction layer, and it has a ton of drivers, including PostgreSQL, Oracle, MSSQL, and MySQL. If you use its built in prepare and execute methods, it will escape data for you, which is very handy. Another positive feature is that it will help you avoid the multitude of mysql_real_add_slashes_im_serious_this_time_i_mean_it methods. I'm also a fan of its placeholder functions, which some databases don't support natively.

Also, if you can, turn off magic quotes. It still comes turned on by default and the only thing that's magic about it is how much it pisses people off.

Adbot
ADBOT LOVES YOU

Xenos
Jun 17, 2005

Scarboy posted:

Isn't PHP a good enough templating language that you shouldn't have to use another one? What's wrong with:

<?

//domain/controller logic

//template

?>

You can just declare all the variables you're going to use in your template(s) in the logic section and just present in the template.

I also agree that MDB2 is super useful, I love that thing.

It does a little more than that, it has built in validation, pagination, it auto-generates drop-down menus, radio groups, checkbox groups as well as templating. I know it seems kinda goofy to have a templating language inside of a templating language at first, but I really like it.

Xenos
Jun 17, 2005

Grigori Rasputin posted:

Also, is using URL arguments like this the preferred way to communicate between pages? It's very easy to do, but I wasn't sure if there was a better way.

This is really a broad question.

Data sharing between pages can be done in a lot of different ways, but it really depends on what you're trying to do. If you're trying to make a form of sign-on system, the most common way to go about that would be to store some info in a session file server-side (PHP does this via session_start and $_SESSION), then have your pages check for the given session info that you set when the user logged in.

When it comes down to accessing info, such as a page to edit a user's profile page, using the query string is the way to go. It would be something like profile.php?profile=blah, which would contain a form that submits to itself via POST. When it comes down to it, I'm just arguing for normal usage of GET and POST -- GET is used to access resources non-destructively, POST is used to modify them. All I really try to do is use the HTTP methods for what they were designed to do in the basic sense, but I'm straying off topic so I'll stop there.

I hope this helps you out :)

Xenos
Jun 17, 2005

The1ManMoshPit posted:

PHP loads a copy of the file into memory once for every request coming in even though I am returning the exact same file for each of these requests. This quickly overwhelms the server if a large number of requests come in simultaneously. Ideally there would be some way for me to just have the file loaded into memory already and have each instance of the script just read and output straight out of that memory.

Are you running against Apache? If so, do you have the ability to install new Apache modules? If so, give mod_xsendfile a try. This module offloads the actual serving of the file to the web server. Instead of reading the file and and outputting its contents, all your script has to do is emit a few headers.

Xenos
Jun 17, 2005

Elias_Maluco posted:

So I have this site thatīs basically an extranet where users will login and download some files.

The files are located outside the public_html, so they cant be linked directly. When the user asks for a file, Im doing like this:

code:
$data = file_get_contents(FILE_PATH.$fname);

header('Content-type: '.getFileExtension($fname));
header("Content-Disposition: attachment; filename=$fname");
echo $data;
Its not the best way, but it works. But now the client uploaded a 1.3 Gb file and then of course file_get_contents will fail for hitting the memory limit.

I cant raise the PHP memory limit anymore than I already did, so it can never qwork this way. What can I do then?

EDIT: as a temporary fix, Im copying the file to a folder inside public_html and then I redirect to its URL. It works but also leaves this copy were it can be downloaded by anyone, so its not a good solution of course.

Are you using Apache? If so, give mod_xsendfile a try. nginx and lighttpd have this as well.

If you can't use this module for whatever reason, you can always read the file in chunks and echo the chunks one at a time instead.

Xenos fucked around with this message at 14:28 on Mar 25, 2011

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