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
waffle iron
Jan 16, 2004
I would either:

A) Use a database abstraction layer with prepared/parametrized queries so you don't have to do the escaping manually or
B) Break the string concatenation on to another line and assigned it to a variable with some line breaks/formatting around the . operator.

Adbot
ADBOT LOVES YOU

waffle iron
Jan 16, 2004

Treytor posted:

I suck at PHP, I admit. All I really know how to do is modify code already given to me, and somewhat follow what is going on. Anyway, let me cut to the chase.

How do I do this:

code:
Without using curl, but fopen instead?
You're going to need to use stream_context_create() to create a context and use that with fopen.

http://us2.php.net/stream_context_create
http://us.php.net/manual/en/wrappers.http.php (See example 1)

waffle iron
Jan 16, 2004

Little Brittle posted:

I can't figure out how to get wrappers working, so I'll just use CURL. Thanks for the tip.

I have another question, how do you get the value of an attribute via XPATH? I have an xml file that looks like this, and I'm trying to grab the ref[href] value (hello.php). Nothing I've tried doesn't seem to be working, and I can't find any good tutorials on it.
code:
<ASX Version="3">
<ENTRY>
<REF HREF="hello.php"/>
<MBLINK HREF="http://www.example.com" />
</ENTRY>
</ASX>
The lazy way is //REF/@HREF and that will give you a nodeset of all HREF attributes of REF tags.

You need the // to search all nodes for the REF tag otherwise the query is /ASX/ENTRY/REF/@HREF

In most engines the tag/attribute name matching can be case sensitive, so check first.

waffle iron fucked around with this message at 04:34 on Jul 5, 2008

waffle iron
Jan 16, 2004

Stephen posted:

How would I check in PHP if a string exists as a variable? isset will only tell me if a variable exists, and does not accept a string as a parameter.
While the solutions above are right, it makes me wonder if the real problem isn't the architecture of the code. If you're using register globals that's probably a bad idea. If you just need arbitrary data storage with string names, a better idea would be to use associative arrays (i.e. $data["foo"]).

waffle iron
Jan 16, 2004

functional posted:

You may be confused because I took the time to write a nice problem description. (Nobody does homework in PHP. And I am not a student.)
gently caress you and your bonus credit.

waffle iron
Jan 16, 2004

weekoldsushi posted:

code:
$file_filename = $_POST['filename'];
$basename = substr($file_filename, 0, strripos($file_filename, '.')); // strip extention
$ext = substr($file_filename, strrpos($file_filename, '.') + 1);
It's kind of frustrating since I don't know how to word it in Google.
Why don't you use basename proper?

http://php.net/basename

waffle iron
Jan 16, 2004

thedaian posted:

I've been talking with Gibbon on AIM for a while, and I'm pretty sure we've solved the problem. Though, Gibbon, I urge you to learn how to program with another language. PHP is a pretty terrible beginners language, and there's a million issues of security and other things that could happen. Plus, even though PHP has pretty good documentation, a lot of tutorials are either badly written, or ignore a lot of the security problems that can exist.
Post of the year. Really helps everyone who comes to ask a question in this thread. :rolleyes:

waffle iron
Jan 16, 2004
That is a pretty horrible way of filtering because it will match on anything that contains f, u, c, and k in that order, whether it be a word or a paragraph.

Edit: Actually it isn't but the you have people putting in underscores or spaces and the whole filtering falls apart again.

waffle iron fucked around with this message at 05:11 on Sep 3, 2008

waffle iron
Jan 16, 2004

Stephen posted:

I wanted to do this, however I'm afraid of creating an infinite loop in the event that one of the files is broken or invalid.
Then count how many times you loop and stop at a sane number of retries. Log the ones that still fail for followup.

waffle iron
Jan 16, 2004
Please don't store images in a database. You're better off writing them to a disk and storing the path and filename in the database.

waffle iron
Jan 16, 2004

Roctor posted:

I'm making a replacement for an ad management system at work and the old system stored all the ad images in the database. Legacy code exists to ruin lives.
The real problem with images in databases is that it creates an IO shitstorm if the web server and database server are the same machine.

At least it's not as bad as storing PHP in a database and then eval'ing.

waffle iron
Jan 16, 2004
From what I remember, the OO stuff in PHP4 is a little slower than straight up procedural programming, but then again objects in PHP4 suck. In early versions of PHP5 there was more overhead for objects, but they were 1st class OO. At this point PHP5 has matured quite a bit and hardware is fast enough so it really should make a difference on performance.

At the same time, I wouldn't overarchitect things by writing your own framework where everything that can be a class is a class. For a lot of stuff, letting the database deal with big work can and is faster.

waffle iron
Jan 16, 2004

MrMoo posted:

If you are using PHP for performance on execution rather than development something is already rather wrong.
:waycool: post.

waffle iron
Jan 16, 2004
I've always been a fan of privilege separation; using different database user accounts with limited permissions to write. Although practically it's easy to go overboard create excess database connections. Certainly having a user that can only read and write, but not alter/drop tables is a good idea. It doesn't protect you from deleting every row though, so use prepared statements.

waffle iron
Jan 16, 2004
When the PHP file it is interpreted and then becomes OPCODEs that are excuted in the Zend Engine. The code should generate the same OPCODEs regardless of formatting.

waffle iron
Jan 16, 2004

the littlest prince posted:

I'm a total php noob, and I want to do some simple site-scraping to make a page for my own personal use. What I want to do is run a search on a couple of craigslist sites (e.g. new york, los angeles, and denver) and then display the results all on one page.

So, step 1 is to get the contents of one craigslist site so I can tell whether the way I'm going about pulling a page is working. Supposedly, file_get_contents should do the trick.

php:
<?
$a = file_get_contents("http://dayton.craigslist.org/");
if ($a == false)
   echo "no good";
else
   echo "ok";
?>
But, this isn't working, as it's always false. I've tried pretty much every example on the page I linked above, but nothing stuck. So I also tried the example on this page (#1), but it didn't work either. Any idea what I'm doing wrong?
Craigslist doesn't like people doing mash ups with their stuff. You probably need to have PHP send a UserAgent that would come from a browser.

See http://www.php.net/manual/en/filesystem.configuration.php#ini.user-agent

waffle iron
Jan 16, 2004
And when it comes down to it, do the null checks yourself or write a helper function that does all that for you. It's not especially hard to get the behavior you're looking for.

The implicit conversions are defined in the documentation for PHP, so it's not like you have to figure this out on your own.

Edit: Next you'll be complaining about the lack of unsigned ints in Javascript and PHP.

waffle iron fucked around with this message at 06:04 on Feb 2, 2009

waffle iron
Jan 16, 2004
If you're going to serve up random images or other poo poo like that, never ever output it in a PHP file unless they're really loving tiny. Because you're not sending Cache headers the browser redownloads it every time.

You're better off writing to a folder if the image is dynamic and then sending a 302 or 307 HTTP status with a Location header.

I repeat: using PHP to send binary data is extra retarded. That goes double for serving images out of a database with PHP.

waffle iron fucked around with this message at 02:26 on Jul 21, 2009

waffle iron
Jan 16, 2004

DarkLotus posted:

Yeah, storing images in a db is retarded.
The only way it might be useful is if you write out images too static web server on the first request, store that filename in a table and serve it out as a Location header.

The moral of the story is static content or content that changes less than once a day should be served off a static web server or cached to disk and served from a static web server.

waffle iron
Jan 16, 2004

supster posted:

This is way too wide of a generalization. There are a lot of legitimate reasons for doing it. Additionally, just because you're serving the file through PHP doesn't mean you can't set cache-control or expires headers - in fact it gives you more control to make better use of them.
Oh man you're hilarious. You think people actually send e-tags or expires headers when they're coming to the internet for help.

waffle iron
Jan 16, 2004

awdio posted:

Why can't I get this simple script to work? All I want to do is take an image from a URL and store it to a folder on my server. The folder has the correct permissions and I've tried a manual image uploader to that same folder and it works.

I've also wrote $fp as open('http://www.majoroutput.com/sonicCircus/images/', 'w'); (without the filename).

php:
<?php
$contents 'http://www.majoroutput.com/sonicCircus/test.jpg';

echo "<img src='".$contents."'>";

$fp fopen('http://www.majoroutput.com/sonicCircus/images/test.jpg''w');
fwrite($fp$contents);
fclose($fp);
?>
edit: I've also tried $contents = file_get_contents('http://www.majoroutput.com/sonicCircus/test.jpg');
Are you trying to use the http file io wrappers to write? If all these files are on your webhost, why don't you just use copy? It also looks like you're passing the wrong arguments to fwrite.

http://php.net/fwrite

waffle iron
Jan 16, 2004

Lumpy posted:

You use them for very different things in general, so it's not really an "instead" thing.

EDIT: as for the discussion du jour, I used to be an echo guy, but I have come to love the printf
I'm a real big fan of printf and sprintf and vsprintf. The formatting abilities make it easier to read. Also useful if you end up having to do localization later.

waffle iron
Jan 16, 2004

fletcher posted:

This may be a dumb question, but what is to stop me from putting a fake login on some site that claims to be an "OpenID login" and just stealing a bunch of OpenID credentials?
With OpenID you give it a personal URL and then it hands it off to your OpenID provider. Then the result from your provider links you back to a URL at the original site with an authentication token. At least that is my understanding.

waffle iron
Jan 16, 2004

thedaian posted:

You can do it. You probably have the syntax wrong (it's also not the best method, but it would work). No need for eval()

php:
<?
function red()
{
    echo 'RED!';
}

$color='red';

$color();?>

I see you and raise you:

php:
<?
function red()
{
    echo 'RED!';
}

function green()
{
    echo 'GREEN!';
}

$red = 'green';
$color='red';

$$color();?>

waffle iron
Jan 16, 2004
When you do a GET on any file, a sane web server will open the file and hold onto that file descriptor and fread()s the gently caress out of it. Saving over a file on a sane OS will unlink that filename and then save a new file with that filename. There is probably no way you'd ever get an incorrect/incomplete file unless the people providing this service are brain dead.

Adbot
ADBOT LOVES YOU

waffle iron
Jan 16, 2004
A location header after sending a file is out of specification.

For the issue of sending the file, I would recommend using X-Sendfile instead of using PHP to read it out. On large files PHP could hit the memory limits and die in the middle execution.

http://codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/

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