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
admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

duz posted:

You can use a combination of HTMLTidy to clean up the HTML and SimpleXML to search the document.

php:
<?
$config = array(
 'indent'         => true,
 'output-xhtml'   => true,
 'wrap'           => 200);
$tidy = new tidy;
$url = 'http://www.example.com/index.html';
$tidy->parseFile($url, $config);
$tidy->cleanRepair();
$doc = new DOMDocument();
$doc->loadHTML($tidy);
$html = simplexml_import_dom($doc);
$h1 = $html->xpath('//h1[@id="main"]');
?>

That's useful if you are parsing an entire html document or a whole bunch of different tags, but if it was a one-time thing I'd probably just use
php:
<?
preg_match("/<h1>(.*?)<\/h1>/s",$string,$h1);
$contents = $h1[1];?>
or something for simplicity

Adbot
ADBOT LOVES YOU

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Treytor posted:

Alright I am a total newb at PHP and coding, and frankly I can't wrap my head around it. But I'm having a problem:
The function call:
php:
<?
checkLog($REMOTE_ADDR,$ipLog,$timeout);?>
should be:
php:
<?
checkLog($_SERVER['REMOTE_ADDR'],$ipLog,$timeout);?>

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Treytor posted:

Wow, that was easy. Thank you sir!

Haha, no problem.

Mistakes like that tend to be extra difficult to catch when starting out. Unsureness leads to checking the logic of your code 30 times over before realizing that it was just a stupid typo.

Scaevolus posted:

Be aware that this is a very inefficient way to do this, and it would be better to use a database.

I'm assuming he's just trying to learn how to program or something, not implement this (I hope).

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

cka posted:

Also, I'm not too sure if it would matter or if it's just the OCD freak in me, but your math could use a quick fix here:

if ($now < ($subdata[1]+3600*$timeout) && $ip == $subdata[0])

should be

if ($now < ($subdata[1]+(3600*$timeout)) && ($ip == $subdata[0]))

in my mind, the first equation would be a loving huge number if your timeout was 2 hours because it would basically multiply the time + 1 hour by two...

Well then apparently your mind doesn't follow simple PEMDAS (and it really, really should if you're working with programming languages).

I like the first version much better personally; it's not cluttered with extraneous parentheses.

admiraldennis fucked around with this message at 08:46 on Mar 25, 2008

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

DaTroof posted:

They're equal in practice because the multiplication is performed before the addition.

They're equal. End of story.

($now < ($subdata[1]+3600*$timeout) && $ip == $subdata[0]) is in no way ambiguous.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

cka posted:

Just my OCD then, no harm no foul. Also, just FYI, I actually was following BEDMAS (same thing as PEMDAS, evidently) with those extraneous brackets around the 3600 * $timeout.
I was talking about this:

cka posted:

in my mind, the first equation would be a loving huge number if your timeout was 2 hours because it would basically multiply the time + 1 hour by two...

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Treytor posted:

Well... I am looking to implement this. I realize a sql database would be much better, but that is WAY beyond me at the moment. One step at a time here...

You sound like you're in a little bit over your head here.

Let me guess, this is for that "prank call people from the internet" thing? I think I recognize you.

If so, you should probably consider paying somebody to do the code correctly for you instead of asking a bunch of questions here just so you can use some crappy file database that's going to choke to death if your site ever gets a lot of traffic.

admiraldennis fucked around with this message at 09:22 on Mar 25, 2008

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

DaTroof posted:

I disagree. One of the benefits of using classes is the ability to use simple, descriptive member names that won't collide with the same name in other contexts. If $obj->delete() is an appropriate name for what the function does, I'd use it. The fact that it's obviously an object member instead of a global function should eliminate any confusion.

I agree, though delete() isn't even a built-in function...

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Good job reading the page you linked

quote:

This is a dummy manual entry to satisfy those people who are looking for unlink() or unset() in the wrong place.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Foolio882 posted:

True, but getting rid of my "html nonsense" changes nothing as well:

php:
<?php header('Content-type: image/jpeg');
 
include("functions.inc");
 
displayPictureTable(shedsgable);
 
?>


You clearly have some fundamental misunderstandings here.

What are you trying to accomplish? What does displayPictureTable output?

By specifying a content-type of image/jpeg, you are telling the browser that the entire document you are producing is a jpeg image. The only output should be binary image information, such as that outputted by gd's image creation functions.

You can't simply spit the binary contents of an image from a gd function into the middle of an html document and expect an image to appear.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Foolio882 posted:

Oh dear god I think I'm hosed, as the function name suggests, this is supposed to be outputting a picture table and it's loaded with required html.

Is there any way to mix html and php to output a table of pictures? I'd like to avoid iterating through the directory to display them as html pictures, as I need the displayed pictures to be thumbnail links to fullsize pictures.

Something like this would work, but be a complete and total pain in the rear end:

php:
<?
$directory = opendir($picPath);

while($fileName = readdir($directory)) {
    $dirArray[] = $fileName;
}

//Here display $picPath/thumbs/$dirArray[1,2,...n] as links to $picPath/$dirArray[1,2,...n]
?>
You see my anger, I'd then have to make sure all the file names were identical and have to create a thumbnail every time we want to add a picture to the table as opposed to just uploading it at the correct resolution into one folder and dynamically resizing it.

This is going to suck.

Web browsers request and load images separately from HTML.

I'd probably stick the resize image code in another php script; let's call it makeThumb.php for example. makeThumb.php takes in an image filename via GET and outputs the resized version.

The displayPictureTable function would output a table containing HTML that refers to images generated by makeThumb.php, for example:
php:
<?
foreach($imgarray as $img) {
    echo "<img src=\"makeThumb.php?img={$img}\"><br>";
}?>
Of course, dynamically resizing all the displayed images every time the page loads is going to be slow as all gently caress, so I'd probably have makeThumb cache the thumbnails we've already created.

An elegant solution might include <img> tags that refer to images directly as if they already existed in a thumbnail directory (e.g. <img src="/img/thumbs/pic1.jpg">). If the appropriate thumbnail already exists, it will be loaded per normal. If not, a simple 404 redirection could point to a thumbnail generation script that would fetch the original image ('/img/pic1.jpg'), return the resized one to the browser, and then store the resized one in the thumbs for later use.

admiraldennis fucked around with this message at 22:01 on Mar 31, 2008

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Foolio882 posted:

If it can be done automatically, I'm all for it and that will work just fine and dandy. I just did a bit of quick googling and found some very relevant information, and hopefully this will all come together nicely.
(Read: I'll be back later with more awful questions, but you all have been very helpful and I do appreciate it!)

Yeah, this is all pretty basic knowledge stuff.

Did you read/comprehend my post?

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Foolio882 posted:

The one part of your post I didn't fully comprehend was "a simple 404 redirection could point to a thumbnail generation script that would fetch the original image ('/img/pic1.jpg'), return the resized one to the browser, and then store the resized one in the thumbs for later use. " I understand the goal there, and think it would work very well, but can you clarify what I've bolded? Is that what you already talked about with the makeThumb.php script?

By "a simple 404 redirection" I meant a system to redirect 404 errors that occur during requests to documents the thumbs directory to a specific php script. If the server was apache, you'd place an .htaccess file in the '/img/thumbs/' directory containing something like:
code:
ErrorDocument 404 /makeThumb.php
That will redirect all 404 (resource not found) errors that occur in that directory to /makeThumb.php. If you request '/img/thumbs/boon.jpg' and boon.jpg exists, it will be returned. If boon.jpg does not exist, it will load makeThumb.php, which will do the following:

  • parse the filename (boon.jpg) out of the request uri (/img/thumbs/boon.jpg)
  • look for the original image file at /img/boon.jpg
  • if the image is not found, display a prepared error image and exit. otherwise, if the image exists, continue on...
  • load and resize the original image
  • display the resized image (use an image content-type and output)
  • store the resized image as a file at /img/thumbs/boon.jpg
A potential flaw in the above is that changing the source images will not update the thumbnails. Your best solution for that is probably either hashing or moddate checking (you'd have to be very careful with the latter but it would be faster).

admiraldennis fucked around with this message at 22:58 on Mar 31, 2008

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Treytor posted:

This is an ip logger and checker, which outputs to a text file on the server. Is there a more efficient way to do this? Say for example if my site was getting hammered and my host was complaining about the PHP slowing down the entire server...

:bang:

Let me quote myself from a few pages ago:

admiraldennis posted:

you should probably consider paying somebody to do the code correctly for you instead of asking a bunch of questions here just so you can use some crappy file database that's going to choke to death if your site ever gets a lot of traffic.
Do you have mysql access on your webhost?

I know you "want to do it yourself" but seriously if your site is getting hammered and your host is complaining just buck up and either 1) go learn to do this with a database, or 2) have somebody else do it for you and study the code and db schema to figure out how it works.

admiraldennis fucked around with this message at 08:37 on Apr 2, 2008

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Finite posted:

...

...

I had no idea you could do that.

Thats the only way one should include variables within a double-quoted string, in my opinion. Either that or with a concatenation. It's borderline ambiguous otherwise and hurts readability.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Zorilla posted:

I think the PHP documentation says the same thing. In other words, something like this?

php:
<?
// Pretend a MySQL connection is open already

$cleanquery = mysql_real_escape_string(stripslashes($query));
$result = mysql_query($cleanquery);
?>

That's what I usually do if magic quotes is on.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

drcru posted:

How do I search for <br /> with preg_match_all?

preg_match_all("/<br \/>/i",$in,$out);

(case-insensitive, will only match <br /> and not <br>)

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

drcru posted:

Can I assume that #\n# will find all new lines?

Yes.

If you have any more questions you should probably just google a regular expressions tutorial.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

gibbed posted:

If you're serious about using a regular expression to find newlines, you probably want #\r?\n|\r# instead.

You should probably just normalize newlines from the get-go with something like:

$str = str_replace(array("\r\n","\r"), "\n", $str);

especially since PCRE by default understands \n as the newline character

admiraldennis fucked around with this message at 06:21 on Apr 9, 2008

Adbot
ADBOT LOVES YOU

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

genericadmin posted:

Or, if you want to be flexible...

preg_match_all("#<br(?: /)>#", $string, $m)

Don't you mean:

preg_match_all("#<br(?: /)?>#", $string, $m)

edit: can't quote your &lt;

admiraldennis fucked around with this message at 12:08 on Apr 9, 2008

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