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
Found Your Answer
Jul 9, 2004

That's like killing a unicorn!
Hm, most of the pages on my site are .html, but I don't want to change them all to .php (and go through and edit the htaccess to that too); is it kosher to do something like

<img src="stats.php" class="nodisplay" />

so that I don't have to do a PHP include on each page/change the extensions/HTA?

stats.php will obviously log browser data/counts.

Adbot
ADBOT LOVES YOU

cka
May 3, 2004
That should work fine, just make your php script dump out a image or something (1x1 transparent gif would suffice) so you can avoid stuff like the red X on IE:

php:
<?php
/* stats script doing stuff here 
   ...
*/
header("Content-type: image/gif");
echo file_get_contents("/path/to/transparent.gif");
?>

The only thing you gotta be wary of is whitespace in your file (particularily after the closing php tag), as that may screw the script up and make it give off the "headers already sent" warning.

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.

Munkeymon posted:

I guess you could do this:
php:
<?
$pipe = popen('tail -opt /path/to/logfile','r');
while(!feof($pipe))
{
    //print stuff here
}
fclose($pipe);
?>
That would never close if tail never died, but I guess if thats what you think you want then whatevs.



That's what I want.

We are getting a bunch of SQL injection attacks on our web site, so I want to write a script that pulls the source IP address from the IIS log file as it happens and keep a tally of them per hour.

This seemed the easiest way of doing it.

Found Your Answer
Jul 9, 2004

That's like killing a unicorn!

cka posted:

That should work fine, just make your php script dump out a image or something (1x1 transparent gif would suffice) so you can avoid stuff like the red X on IE:

php:
<?php
/* stats script doing stuff here 
   ...
*/
header("Content-type: image/gif");
echo file_get_contents("/path/to/transparent.gif");
?>

The only thing you gotta be wary of is whitespace in your file (particularily after the closing php tag), as that may screw the script up and make it give off the "headers already sent" warning.

Ah, good idea, I hadn't thought about that (though I don't know if said red X will appear if the "image" is display:none'd).

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS

windwaker posted:

Ah, good idea, I hadn't thought about that (though I don't know if said red X will appear if the "image" is display:none'd).

It'll still show up in browser validation extensions and stuff, so you may as well do it.

MononcQc
May 29, 2007

windwaker posted:

Ah, good idea, I hadn't thought about that (though I don't know if said red X will appear if the "image" is display:none'd).
Opera isn't supposed to load images in display:none; so if you want good stat accuracy, I guess you have to do something else.

eHacked
Sep 30, 2003

CONGRATULATIONS!!! YOU ARE THE 6,127,436,218TH PERSON TO VIEW THIS USELESS POST. CLICK TO CLAIM YOUR PRIZE!!!
Does anyone know how to check if a remote file exists?

I can do it with file_exists() but only on local files ...

I need my script to check if x.swf exists on a remote web address, but don't want it to load the full file, only, say 50 bytes or so so that the visitor won't notice any lag.

would fread accomplish this? fopen in php.net doesn't seem to have good examples of this.

Stephen
Feb 6, 2004

Stoned

eHacked posted:

Does anyone know how to check if a remote file exists?

I can do it with file_exists() but only on local files ...

I need my script to check if x.swf exists on a remote web address, but don't want it to load the full file, only, say 50 bytes or so so that the visitor won't notice any lag.

would fread accomplish this? fopen in php.net doesn't seem to have good examples of this.
I've done this before using cURL by checking the response sent back from the server for errors. It worked, but there's probably a lot better ways to do it.

Zorilla
Mar 23, 2005

GOING APE SPIT

Stephen posted:

I've done this before using cURL by checking the response sent back from the server for errors. It worked, but there's probably a lot better ways to do it.

Probably not. If HTTP is the only means of communicating with that server, then your way is ideal.

Lank
Sep 16, 2002

WHERE IS THE CHANCELLOR?!

I'm going out of my mind with this one.

I have a csv that's generated from a website that I need to read in to a sql table. I cannot control the file coming to me as far as how it's encoded etc.

Right now, when I use fopen, fgets and then a little csv parser I wrote, and then add the rows to my sql tables I'm having an issue. On both the echo statements I use for debugging in firefox and in the actual table itself there is a little black diamond with a question mark in it in between every single character.

If I open said csv in notepad, resave it as ANSI encoding and run the exact same process, it works fine.

I did enough research to know this has to do with character sets and how the file is encoded, but I was pulling my hair out trying to get this to work.

I used utf8_encode and utf8_decode on the string coming in after fopen and before converting it into an array for processing.

I tried iconv(), mb_convert_encoding(), htmlentities(), htmlspecialchars(), adding the charset to use in a header statement on my php page and just about any other fix I could find regarding unrecognized characters. None of them worked. Some got the first value in the array to be fixed and turned the rest into blank, some added more ?s, some did nothing, one even changed it all into japanese characters.

Most of the solutions I found were regarding changing the ?s into what they were originally coming in as, like foreign language characters or something. I want them gone. When I view the csv in any text editor, I can see all the data just fine sans any unrecognized characters or whitespace.

I know this might have to be with me not understanding encoding and decoding different filetypes and making my website UTF-8 compliant but I figured it was a good time to learn.

Standish
May 21, 2001

Lank posted:

Right now, when I use fopen, fgets and then a little csv parser I wrote, and then add the rows to my sql tables I'm having an issue. On both the echo statements I use for debugging in firefox and in the actual table itself there is a little black diamond with a question mark in it in between every single character.


If I open said csv in notepad, resave it as ANSI encoding and run the exact same process, it works fine.
If every single character (even regular alphanumeric characters) has a black diamond before it then it sounds more like UCS-2 encoding than UTF-8. Try
php:
<?
iconv("UCS-2","UTF-8", $string);?>

quote:

I have a csv that's generated from a website that I need to read in to a sql table.
Check out the response headers on the website you're downloading the CSV from, is it sending a particular character set in the Content-Type?

Standish fucked around with this message at 20:48 on Sep 10, 2008

Lank
Sep 16, 2002

WHERE IS THE CHANCELLOR?!

Standish posted:

If every single character (even regular alphanumeric characters) has a black diamond before it then it sounds more like UCS-2 encoding than UTF-8. Try
php:
<?
iconv("UCS-2","UTF-8", $string);?>
Check out the response headers on the website you're downloading the CSV from, is it sending a particular character set in the Content-Type?

That might be one of the few combinations I didn't try. I tried using UTF-16LE to UTF-8 but that didn't do the trick. I'll attempt the UCS-2 conversion and come back here with header information if it doesn't work.

The site the csv is coming from has a little "Generate report!" button and just spits out a download prompt when it creates the file so I'm not sure how much information I can get on whether the file is screwy or not. I tried using mb_detect_encoding or whatever that function is and it was saying the strings were ASCII...so that didn't help me very much, but I can dig some more.

Am I corrent in thinking USC-2 is kind of archaic? Thanks for the suggestions, I'll give it a shot.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Lank posted:

The site the csv is coming from has a little "Generate report!" button and just spits out a download prompt when it creates the file so I'm not sure how much information I can get on whether the file is screwy or not.

LiveHTTPHeaders for Firefox should let you see what it's being sent as if that is set correctly.

Leavy
Feb 15, 2003

rufus

Lank posted:

I tried using mb_detect_encoding or whatever that function is and it was saying the strings were ASCII...so that didn't help me very much, but I can dig some more.
Did you run the detect function on the whole file contents or a piece of the file?
If you haven't, I'd recommend trying the function on just one field of the csv import instead of the whole thing.
Formally I know very little about character encoding, but I know an awful lot from banging my head against it incessantly. I believe that the file could be structured in that the csv file structure is in ascii, but each field of data is encoded in the weirdo encoding.

Lank
Sep 16, 2002

WHERE IS THE CHANCELLOR?!

Leavy posted:

I believe that the file could be structured in that the csv file structure is in ascii, but each field of data is encoded in the weirdo encoding.

Oh that's special. Haha. Well I've got lots of new things to try out. Thanks a ton. If none of that works then screw it, part of the SOP will be "Open in textpad, save as ANSI".

Vince McMahon
Dec 18, 2003
Not sure if this is a PHP question or a mySQL question really..

Basically I'm trying to store a number in a decimal field in my mysql database. The number is greater than 0, and I need a fair few decimal places. The field size is 20,20. Now, I'm trying to store my number in there, but it seems to misinterpret it and I don't know why. The code is like this:

code:
$user_digitratio = number_format(($_POST['indexfinger'] / $_POST['ringfinger']), 2);

$querytext = "INSERT INTO users (forename, surname, sex, handed, digitratio) 
VALUES ('".$user_forename."', '".$user_surname."', '".$user_sex."', '".$user_handed."', ".$user_digitratio.")";
Now, I echoed the query before it processes it, and the $user_digitratio variable comes out as it should. I made the ratio 2.00, it echoed 2.00, but it was stored as 0.999999999999.

I've never coded PHP before, but I'm used to ASP and it's not a lot different, so this is a bit confusing to me. If I did essentially the same code in ASP I'm sure it would work properly. Any help is appreciated.

edit: nevermind, fixed it. I changed the field to double and fixed the size limits... I think mysql dislikes the lengths being the sames sizes.

Vince McMahon fucked around with this message at 21:30 on Sep 11, 2008

jasonbar
Apr 30, 2005
Apr 29, 2005

Vince McMahon posted:

edit: nevermind, fixed it. I changed the field to double and fixed the size limits... I think mysql dislikes the lengths being the sames sizes.

MySQL reads the definition as SIZE, PRECISION, so 20,20 would be a column that holds 20 digits, with 20 digits of precision (all of the digits.)

For example, a cost column would be decimal 5,2. It would hold up to 999.99.

http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html

jasonbar fucked around with this message at 23:05 on Sep 11, 2008

Vince McMahon
Dec 18, 2003
Yeah that was the problem. In my head I think I saw 20,20 as 20 digits infront of the decimal, 20 behind. Oops!

Stephen
Feb 6, 2004

Stoned
What is the best way (if any) to carry sessions over domains? E.g. My users will login to admin.something.com. Options will be given to them based on what sites they have access to such as new1.something.com or new2.something.com

Since they need to login to admin.something.com I don't want to make them re-login when they select a page they have access to.

SuckerPunched
Dec 20, 2006

Stephen posted:

What is the best way (if any) to carry sessions over domains? E.g. My users will login to admin.something.com. Options will be given to them based on what sites they have access to such as new1.something.com or new2.something.com

Since they need to login to admin.something.com I don't want to make them re-login when they select a page they have access to.


Set a cookie with the domain ".something.com"

http://us2.php.net/manual/en/function.setcookie.php

Stephen
Feb 6, 2004

Stoned

SuckerPunched posted:

Set a cookie with the domain ".something.com"

http://us2.php.net/manual/en/function.setcookie.php
Yeah I figured on doing it this way, it just seems a little insecure. I guess if I stored the session ID and the IP address in the database and matched them each time the user browses to a page, it wouldn't be so bad.

Standish
May 21, 2001

Stephen posted:

I guess if I stored the session ID and the IP address in the database and matched them each time the user browses to a page, it wouldn't be so bad.
You can't assume in general that a user will retain the same IP address for the length of their session, it breaks for a lot of ISPs that put their users behind transparent load-balancing proxies. (You can't even assume the top /16 or /24 bits of the IP will be the same, I've run into situations where consecutive requests from the same user came from totally unrelated IPs.)


If you want to protect your users against cookie sniffing/session hijacking, use SSL (and set the "secure only" attribute on your cookies).

Stephen
Feb 6, 2004

Stoned

Standish posted:

You can't assume in general that a user will retain the same IP address for the length of their session, it breaks for a lot of ISPs that put their users behind transparent load-balancing proxies. (You can't even assume the top /16 or /24 bits of the IP will be the same, I've run into situations where consecutive requests from the same user came from totally unrelated IPs.)


If you want to protect your users against cookie sniffing/session hijacking, use SSL (and set the "secure only" attribute on your cookies).
That's a really good point. Thanks for the tip. This means I'll have to enabled SSL across all of the sites, yes?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Stephen posted:

That's a really good point. Thanks for the tip. This means I'll have to enabled SSL across all of the sites, yes?

If you're trying to make it as secure as possible, yes.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I'm trying to use this PHP Serial Extension following this reference for XAMPP but it's still saying the extension isn't loaded. What could be causing this?

My phpInfo() is showing:

Server API: CGI/FastCGI
Loaded Configuration File: C:\xampplite\php\php.ini

and that is the php.ini where I put:

extension=php_ser.dll

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

fletcher posted:

I'm trying to use this PHP Serial Extension following this reference for XAMPP but it's still saying the extension isn't loaded. What could be causing this?

My phpInfo() is showing:

Server API: CGI/FastCGI
Loaded Configuration File: C:\xampplite\php\php.ini

and that is the php.ini where I put:

extension=php_ser.dll

Did you look in the error log? I am guessing that it is not finding the dll file.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Begby posted:

Did you look in the error log? I am guessing that it is not finding the dll file.

[error] [client 127.0.0.1] PHP Warning: PHP Startup: win_serial_port: Unable to initialize module
[error] [client 127.0.0.1] Module compiled with module API=20050922, debug=0, thread-safety=1
[error] [client 127.0.0.1] PHP compiled with module API=20060613, debug=0, thread-safety=1
[error] [client 127.0.0.1] These options need to match
[error] [client 127.0.0.1] in Unknown on line 0

So I guess it was designed for an earlier version of PHP - any way around this? Is there just a command line program I can call to communicate with the COM port? This also looks promising.

fletcher fucked around with this message at 06:22 on Sep 19, 2008

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
code:
rewriteEngine on
RewriteCond %{HTTP_HOST} ([^.]+)\.url.com [NC]
RewriteRule ^(.*) [url]http://url.com/index.php?subdomain=[/url]%1 [P]
I'm using the above htaccess to make all subdomains point to the root index.php and it's working fine.

I need to pass any further variable on, say http://penis.url.com/testicles as rewriterule http://url.com/index.php?subdomain=%1&switch=%2 - can you fine gentlemen offer me any assistance?

Namen
Mar 23, 2004
BEES! BEES IN THE CAR! BEES EVERYWHERE!
I have a question for you guys. I'm writing a small application for work that basically disseminates information to a few email distribution lists. I'm trying to keep the entire application object-oriented as I can see myself reusing a few of these classes in the future.

My question is in regards to small classes / functions. Does it make sense to create a class that simply provides one function? Would it make more sense to try and fit that function into another class? For example, consider the following function:

code:
function stripInput($inputData) {
	return strip_tags(htmlspecialchars(stripslashes($inputData)));
}
I don't really have a class that does anything specific with string formatting/manipulation, so should I create brand new one that only serves that purpose? I suppose my question is really more about best practices when using object oriented techniques. I'm relatively new to it honestly, and I'm trying to break my procedural habits.

Any insight would be appreciated.

jasonbar
Apr 30, 2005
Apr 29, 2005

xluna posted:

I don't really have a class that does anything specific with string formatting/manipulation, so should I create brand new one that only serves that purpose? I suppose my question is really more about best practices when using object oriented techniques. I'm relatively new to it honestly, and I'm trying to break my procedural habits.

Any insight would be appreciated.

I usually create a static utility class to handle one off things like string manipulation and validation. All of the methods are declared as static, and they do exactly what you have there, accept something, transform / validate it, and return.

That way you can keep all of your utility library functions in one place, if you are utilizing __autoload you can just call Utility::DoSomething($blah) and get the expected result from anywhere without worrying about including anything extra.

jasonbar fucked around with this message at 21:39 on Sep 19, 2008

Namen
Mar 23, 2004
BEES! BEES IN THE CAR! BEES EVERYWHERE!

jasonbar posted:

I usually create a static utility class to handle one off things like string manipulation and validation. All of the methods are declared as static, and they do exactly what you have there, accept something, transform / validate it, and return.

That way you can keep all of your utility library functions in one place, if you are utilizing __autoload you can just call Utility::DoSomething($blah) and get the expected result from anywhere without worrying about including anything extra.

Awesome. Great idea. I didn't know how static methods worked, and this just made life a lot simpler. I knew there had to be a way to simply use class methods without instantiation as you do it all the time in C++, i just wasn't sure how because of my new-ness to it all.

blargle
Apr 3, 2007
I'm a Java Servlet/J2EE guy, and I just read through the PHP documentation on their site. They don't seem to come out and say it directly, but it seems like a new PHP interpreter is created, runs and then dies within the scope of a single http request? (As opposed to Java where it runs continuously and handles incoming requests with threads). The reason I ask is because it would seem to make it tricky to do things like cache database queries in memory or maintain user session in memory. The options seem to be storing session in the browser cookie (bleh), or flat files (slow). Is there a way to make PHP run persistently, or do I just have to change the way I approach writing webapps?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

blargle posted:

I'm a Java Servlet/J2EE guy, and I just read through the PHP documentation on their site. They don't seem to come out and say it directly, but it seems like a new PHP interpreter is created, runs and then dies within the scope of a single http request? (As opposed to Java where it runs continuously and handles incoming requests with threads). The reason I ask is because it would seem to make it tricky to do things like cache database queries in memory or maintain user session in memory. The options seem to be storing session in the browser cookie (bleh), or flat files (slow). Is there a way to make PHP run persistently, or do I just have to change the way I approach writing webapps?

You can use PHP Sessions. Also check out memcache for caching stuff from a database.

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

xluna posted:

Awesome. Great idea. I didn't know how static methods worked, and this just made life a lot simpler. I knew there had to be a way to simply use class methods without instantiation as you do it all the time in C++, i just wasn't sure how because of my new-ness to it all.

Static methods are handy, but don't overuse them. If you do you will make your classes harder to reuse since they are dependent on the implementation and naming of classes. Also, it makes it harder to do unit tests.

I fell into this trap, and ignoring the advice I received on this forum and others dug some deep holes for myself that are still biting me in the rear end.

For instance, you might be tempted to create a database class and call it like mydbclass::query($sql). Then mydbclass might have a singleton db connection or something.

The problem arises when you try to reuse dependent classes in a different application, you have to have mydbclass wherever that class is uses. Or what if you want to query two databases with the same object that depends on mydbclass. You would be up a creek.

So it can be handy, but don't overuse it. Often it is better to pass objects in constructors like this, and to make classes dependent on interfaces:

code:
$db = new mydbclass();
$people = new peopleThingy($db);
That is a lot better than making peopleThingy use a static db class, its more flexible, plus you can unit test it without too much trouble, also you can swap out $db for any other object as long as it implements the same interface.

Zorilla
Mar 23, 2005

GOING APE SPIT
Is there a good way to gracefully reject file uploads that are too large? Limits are usually controlled through php.ini or other files that compliment it such as .htaccess, which means PHP is the one throwing a fit when something is too big.

I want to display a user-friendly error if somebody tries to upload gargantuan, unresized images from their 14 MP camera to a website. My guess is that there is some sort of error you could check for on postback, but Google comes back absolutely dry when I look up information on this. Any ideas?

edit: I hate it when I find the answer 2 seconds after I give up. $_FILES["some_field"]["error"] == UPLOAD_ERR_INI_SIZE (or similar errors) does the trick.

edit2: This only returns errors if individual files go over the collective limit, so it's no good for multiple file uploads. For now, I'm just using a function that adds up all the $_FILES["some_field"]["size"] fields and compares the result to max_upload_size (in integer form). If anybody knows a simpler way, tell me about it.

Zorilla fucked around with this message at 21:22 on Sep 26, 2008

Stephen
Feb 6, 2004

Stoned
I've got three sites all hosted on the same server in separate virtuals. One of the sites is an admin section that should be able to upload pictures etc. to the other sites. I tried writing files using the absolute path of the site on the server, but that failed. I could FTP each file to it's respective site once the user has uploaded it, but that just seems a bit messy.

What are my options (if any) for writing files across sites?

Edit: Solved it by posting the file to the other site, and then redirecting back again.

Stephen fucked around with this message at 16:46 on Oct 1, 2008

Aturaten
Mar 23, 2008

by elpintogrande
Is there any way to get a DIVs size using PHP? I really need to find this out soon, this image gallery is killing me.

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

Aturaten posted:

Is there any way to get a DIVs size using PHP? I really need to find this out soon, this image gallery is killing me.

PHP outputs HTML on the server and sends it to your browser. Your browser then renders the div and creates the size, at that point PHP is done processing.

You would need something client side to read the div. You could however send this back to PHP via javascript or something.

That is unless I am missing your point, if so be more descriptive.

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS

Aturaten posted:

Is there any way to get a DIVs size using PHP? I really need to find this out soon, this image gallery is killing me.

Yeah, more info would be good.

If you want to find the size of a div after the image loads or whatever, it might be a little bit tricky. Try making the divs a fixed size instead.


If you really really really need to do that, however, the best way might be to AJAX it up. There are faster ways, but that would be the easiest. If you need a little more speed for a little more work, though, you can do everything serverside by using GD to find all of the image sizes and adding them up.

Adbot
ADBOT LOVES YOU

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
I have a huge table of articles I've posted on one of my personal sites and each entry has a tags field as such

art_id, art_name, art_cont, art_tags
1, "title of article 1", "content of article 1", "hello, wooop, omnomnom"
2, "title of article 2, "content of article 2", "jello shots, canabis, gaysex"
3, "title of article 3, "content of article 3", "jello shots, canabis, robin hood"
4, "title of article 4, "content of article 4", "canabis, woop, something else, craigslist"

How would I create an array of all the tags, ordered by any more commonly used ones first? Would I implode them all into a tags array, order by somthing then use an array function to remove dupes? or is there some mythical magical MySQL command that'll accomplish this for me?

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