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
er0k
Nov 21, 2002

i like ham.

Safety Shaun posted:

I was using ip2c (free IP to country database) for a while until it broke. Even the new version keeps breaking, I pasted the errors on pastebin.

Has anybody experienced this too, or any idea why?

The PHP files DO point to the database files correctly, which confused me.

Looks like you are using a relative path (fopen(../ip-to-country.bin)), do you still get the errors when you use an absolute path?

Failing that, maybe try Maxmind's GeoIP database, it's been working great for me.

Adbot
ADBOT LOVES YOU

bt_escm
Jan 10, 2001

passionate dongs posted:

Really dumb question:

I'm outputting the results of a mysql query into a page and loading it into a javascript array. Right now it is essentially like this:

code:
var myArray = [ 
<? while(mysql results) {
  echo("{name: $result},");
} ?>
];
problem is, that there is always one stray comma at the end of the last entry into the array, which makes sense. Outside of writing some really bad code, I am fairly certain there has to be an easy way to output "a, b, c, d, e" instead of "a, b, c, d, e, "

The code works in IE/Firefox/Safari, but of course it produces javascript errors. What is an easy way to say "on the last record truncate the comma" ?

put the resultset into an array and then have a look at http://www.php.net/json_encode

MrEnigma
Aug 30, 2004

Moo!

bt_escm posted:

put the resultset into an array and then have a look at http://www.php.net/json_encode

This works great for encoded arrays for javascript, however I think it requires php 5.2 or higher...

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

MrEnigma posted:

This works great for encoded arrays for javascript, however I think it requires php 5.2 or higher...

You can grab json_encode.php off PEAR and just include 'json_encode.php';

MrEnigma
Aug 30, 2004

Moo!

fletcher posted:

You can grab json_encode.php off PEAR and just include 'json_encode.php';

Ironically my own advice from earlier in the thread, and not sure why I didn't think of that, thanks for the tip.

Safety Shaun
Oct 20, 2004
the INTERNET!!!1

er0k posted:

Looks like you are using a relative path (fopen(../ip-to-country.bin)), do you still get the errors when you use an absolute path?

Failing that, maybe try Maxmind's GeoIP database, it's been working great for me.

sample.php, geoip.inc, GeoIP.dat all in the same dir (domain.com/ip2c)
path in sample.php is correct:

$gi = geoip_open("/home/removed/public_html/ip2c/GeoIPCity.dat",GEOIP_STANDARD);

Still not working:


Warning: fopen(/home/removed/public_html/ip2c/GeoIPCity.dat) [function.fopen]: failed to open stream: No such file or directory in /home/removed/public_html/ip2c/geoip.inc on line 314

Line 314 just includes the var $gi


EDIT: Fixed, standard file in the code is GeoIPCity.dat, I just had GeoIP.dat.

Safety Shaun fucked around with this message at 17:28 on Jun 25, 2008

MrEnigma
Aug 30, 2004

Moo!

Safety Shaun posted:

sample.php, geoip.inc, GeoIP.dat all in the same dir (domain.com/ip2c)
path in sample.php is correct:

$gi = geoip_open("/home/removed/public_html/ip2c/GeoIPCity.dat",GEOIP_STANDARD);

Still not working:


Warning: fopen(/home/removed/public_html/ip2c/GeoIPCity.dat) [function.fopen]: failed to open stream: No such file or directory in /home/removed/public_html/ip2c/geoip.inc on line 314

Line 314 just includes the var $gi


EDIT: Fixed, standard file in the code is GeoIPCity.dat, I just had GeoIP.dat.

The only way this could be failing is if you have the wrong path.

I'm guessing not only do you have a user, but also the domain something like

/home/user/domain.com/public_html/ip2c/GeoIPCity.dat

Edit: if you have shell access, go to the ip2c directory and type 'pwd'

MrEnigma fucked around with this message at 17:45 on Jun 25, 2008

G-Dub
Dec 28, 2004

The Gonz
Is there anything wrong with throwing a random die() or exit() in a script? The scenario:

php:
<?

if(condition 1) {
// do some stuff
}
elseif(condition 2) {
// Do other stuff
die();
}
else {
// Other poo poo
}

// Here is some code I want displayed if any condition other than condition 2 is met
?>
There is probably a great problem with my coding theory that lets me get into this situation but killing the script where I do makes it work perfectly. Bad practice?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

G-Dub posted:

Is there anything wrong with throwing a random die() or exit() in a script? The scenario:

There is probably a great problem with my coding theory that lets me get into this situation but killing the script where I do makes it work perfectly. Bad practice?

If you want code run that matches any condition but condition 2, why not:

if (!condition 2) {
}

hey mom its 420
May 12, 2007

That depends on the conditions, really. What's the second condition? If it doesn't evaluate to true, whose fault is that? Why would you want your script to just die instead of sending a useful error header and/or message to the user?

er0k
Nov 21, 2002

i like ham.

G-Dub posted:

Is there anything wrong with throwing a random die() or exit() in a script?

You'll run into problems if another script is dependent on it, but otherwise I don't really see why not. You can die with a useful error message as well.

MrEnigma
Aug 30, 2004

Moo!

er0k posted:

You'll run into problems if another script is dependent on it, but otherwise I don't really see why not. You can die with a useful error message as well.

Yeah, it's probably not the best practice as others have said, but really you can do whatever you want (ie php can work that way). I'd recommend throwing an Exception, and potentially catching it someplace else if needed. Although if you don't catch it, you're probably going to end up with a big ugly PHP error on your page.

Neon Machete
Dec 30, 2006
yes
I'm trying to set up a simple PHP script that uploads a file and inserts the filename into an XML document. I have a slideshow system that fetches the image names from the document and puts it up on the slideshow. I'm using DOM XML functions to do this. The problem is, I'm a total newbie with PHP and my host is using an ancient version of PHP (4.1.2).

My xml file is structured like this
code:
<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album title="Photos" description="Pictures" lgPath="gallery/album1/large/">

<img src="image1.jpg" />
<img src="image2.jpg" />


</album>
</gallery>
All I need to do with my PHP script is insert my image name inside the <album> tags formatted like the others. Uploading works fine. Beyond this, I'm stumped. I've tried different methods for a good 5 hours or so, but nothing works. DOM XML functions vary from version to version but I simply cannot find accurate documentation of them under 4.1.2. It seems really simple based upon the tutorials I have seen for PHP 5. The documentation on the PHP website is all for the later versions of 4. Anyone here well versed in caveman PHP coding?

MrEnigma
Aug 30, 2004

Moo!

Neon Machete posted:

I'm trying to set up a simple PHP script that uploads a file and inserts the filename into an XML document. I have a slideshow system that fetches the image names from the document and puts it up on the slideshow. I'm using DOM XML functions to do this. The problem is, I'm a total newbie with PHP and my host is using an ancient version of PHP (4.1.2).

My xml file is structured like this
code:
<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album title="Photos" description="Pictures" lgPath="gallery/album1/large/">

<img src="image1.jpg" />
<img src="image2.jpg" />


</album>
</gallery>
All I need to do with my PHP script is insert my image name inside the <album> tags formatted like the others. Uploading works fine. Beyond this, I'm stumped. I've tried different methods for a good 5 hours or so, but nothing works. DOM XML functions vary from version to version but I simply cannot find accurate documentation of them under 4.1.2. It seems really simple based upon the tutorials I have seen for PHP 5. The documentation on the PHP website is all for the later versions of 4. Anyone here well versed in caveman PHP coding?

There are various backports of different things, like for instance PHP 5's SimpleXML class, has been backported to php4 (although you'll still need the XML parser extension)

http://www.ister.org/code/simplexml44/index.html

If you get that working, you can use something like:

http://us2.php.net/manual/en/function.simplexml-element-addChild.php

PHP in general isn't very good in dealing with XML, but with a few tricks you can make it work.

G-Dub
Dec 28, 2004

The Gonz

fletcher posted:

If you want code run that matches any condition but condition 2, why not:

if (!condition 2) {
}

That also works I guess, and so simple.

The die/exit doesn't occur in a state of error. Essentially I think I have structured my code in a lovely way due to changing my specification every two seconds. Anyway fletcher's method looks like less of a hack so I will go that way. Was just wondering if die/exit resulted in an error in a log or something

Frocer
Feb 11, 2004
Big Slick

Frocer posted:

Does anyone have or know how to write a script that can grab popular IM online status? e.g. AIM, MSN, Yahoo, etc.

I figured out how to grab user's online status for AIM, but couldn't find anything for MSN, Yahoo, or others... any tips would be greatly appreciated!

Anyone has any idea where to look for this? I have been googling forever and still haven't been able to find anything besides AIM :/

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Frocer posted:

Anyone has any idea where to look for this? I have been googling forever and still haven't been able to find anything besides AIM :/

Have you tried looking at other open source chat clients to see how they do it? It has been done in many languages, just use a non PHP one to guide you in writing it. This one written in perl would probably be useful.

zapateria
Feb 16, 2003
A quick one, that is not so much PHP really, than Apache/SAMBA.

I want my web application to read a file on a samba share, and display it, but it doesn't work.

This might be something silly that I can't see because I'm going crazy over this so if anyone can give me a hint?

My DocumentRoot is /var/www/html

First I tried to just run: exec("tail /mnt/samba/file",$output);

which doesn't work.. So I imagine there is some kind of permission trouble

I tried making a link... "ln -s /mnt/samba/file file" in the /var/www/html dir.. and that doesn't work either of course. Making a hard link gives me a " Invalid cross-device link"

So what's the easiest way to do this?

Is it possible to move the whole DocumentRoot to the samba share?

zapateria fucked around with this message at 11:53 on Jun 27, 2008

RyceCube
Dec 22, 2003
I want to look at designing an application designed to interact with another website. Basically, I want to be able to read data and interact with the other page through PHP. (Automate a control panel which I do not want the user to have full access to) Can anyone point me in the right direction on what I would use to do this?

such a nice boy
Mar 22, 2002

zapateria posted:

So what's the easiest way to do this?

Is it possible to move the whole DocumentRoot to the samba share?

Try running the commands as the "www-data" user. It'll show you the same errors that PHP is getting.

Begby
Apr 7, 2005

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

Phiberoptik posted:

I want to look at designing an application designed to interact with another website. Basically, I want to be able to read data and interact with the other page through PHP. (Automate a control panel which I do not want the user to have full access to) Can anyone point me in the right direction on what I would use to do this?

1. Use curl to download a page from the other site

2. Curl will return the page source as a string. You can then read through this string using regular expressions, or if it is well formed maybe DOM.

3. After you find what you need in the page, you can then send another request back to the site with curl to post data to forms etc.


This could be relatively simple or it could be a total clusterfuck nightmare, it really depends on what you need to do with the other site and how well structured the HTML on that site is.

P.S. If the pages change that you are interacting with, it could break your code, so be careful

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Phiberoptik posted:

I want to look at designing an application designed to interact with another website. Basically, I want to be able to read data and interact with the other page through PHP. (Automate a control panel which I do not want the user to have full access to) Can anyone point me in the right direction on what I would use to do this?

I would make sure you aren't breaking their TOS too

Little Brittle
Nov 1, 2004

Come visit me dawg
How does preg_match() work for whitespace? I have a bit of ugly code that looks like this, which I'm trying to strip the contents from.
code:
<div class="description">
			This is the text description for the item I'm trying to get via CURL.
		</div>
The preg_match() code I'm trying to use looks like this:
code:
$find_description = '/<div class="description">(.+?)<\/div>/';
preg_match($find_description,$html,$description);
$description = strip_tags($description[1]);
But it keeps returning an error. How can I get this working?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Little Brittle posted:

How does preg_match() work for whitespace? I have a bit of ugly code that looks like this, which I'm trying to strip the contents from.

But it keeps returning an error. How can I get this working?

Load it up into a DOMDocument object and extracting it will be a little easier to maintain.

http://www.php.net/dom

Little Brittle
Nov 1, 2004

Come visit me dawg

fletcher posted:

Load it up into a DOMDocument object and extracting it will be a little easier to maintain.

http://www.php.net/dom
I'm having trouble understanding how you would extract information from elements lacking unique IDs or names with DOMDocument. There are a few things I have to grab by class or by a unique combination of tags wrapping around the data I need. Could you explain how you would use DOMDocument to grab that sort of info?

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

Little Brittle posted:

I'm having trouble understanding how you would extract information from elements lacking unique IDs or names with DOMDocument. There are a few things I have to grab by class or by a unique combination of tags wrapping around the data I need. Could you explain how you would use DOMDocument to grab that sort of info?

DOMXPath Class

XPath Tutorial

Example:
php:
<?
$dom = new DOMDocument::loadHTMLFile('example.html');
$xpath = new DOMXpath($dom);

// Get a list of all div elements where class = "foo"
$list1 = $xpath->query("//div[@class='foo']");

// Get a list of all a elements that are inside p elements
$list2 = $xpath->query("//p/a");
?>

DaTroof fucked around with this message at 07:44 on Jun 29, 2008

jasonbar
Apr 30, 2005
Apr 29, 2005

Little Brittle posted:

How does preg_match() work for whitespace? I have a bit of ugly code that looks like this, which I'm trying to strip the contents from.

It looks like you should probably use the DOMXPATH as DaTroof mentioned, but, to answer your question, your regexp should read:

$find_description = '/<div class="description">(.+?)<\/div>/s';

Adding /s will treat the string as a single line.

edit: You might find http://www.weitz.de/regex-coach/ to be a useful tool.

jasonbar fucked around with this message at 08:47 on Jun 29, 2008

Little Brittle
Nov 1, 2004

Come visit me dawg
Thanks for the help, I got it working.

LastCaress
May 8, 2004

bonobo
I just started a competition in my forum. Basically people donate their virtual forum money to me, and I convert it to real money, which I donate to different charities. Is there a simple php script that allows me to have a table of the donators which can be easily updated? Maybe I just need something with "Nick, total donated, number of donations" or something more complex if it exists. Thanks.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

LastCaress posted:

Is there a simple php script that allows me to have a table of the donators which can be easily updated? Maybe I just need something with "Nick, total donated, number of donations" or something more complex if it exists. Thanks.

No, I doubt there is, especially one that integrates with your BB software. You will need a bit of programming knowledge to do this and just write your own. Take a look at basic PHP and basic MySQL tutorials.

LastCaress
May 8, 2004

bonobo

fletcher posted:

No, I doubt there is, especially one that integrates with your BB software. You will need a bit of programming knowledge to do this and just write your own. Take a look at basic PHP and basic MySQL tutorials.

Ah, but I don't need forum integration, I can have a separate table where I manually update it with the nicks and donations!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

LastCaress posted:

Ah, but I don't need forum integration, I can have a separate table where I manually update it with the nicks and donations!

Why do you need anything more than HTML for this?

LastCaress
May 8, 2004

bonobo
I just wanted something that I could easily update, like have a form to input every donation that updated the table automatically.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
I'm playing around with some PHP to make an RSS reader for a webpage, and I'm having a bit of trouble. If I set the function setHead to echo the input as it sets the variable, it will display, but I recieve nothing from getHeadline();


code:
<?php
include("story.php");
$fileName = "../files/sample.xml";
$reader = new XMLReader();
$reader->open($fileName);
$scount = 0;
while ($reader->read()){
   if ($reader->name == "Story"){
      $stories[$scount] = new Story;
      $reader->read();
      $reader->read();
      if ($reader->name == "headline") {
         $reader->read();
         $line = $reader->value;
         $stories[$scount]->setHeadline($line);
         echo $stories[$scount]->getHeadline();
      }
      $scount++;
   }
}
?>


<?php
class Story {
   var $headline = "";
   var $url = "";
   var $source = "";
   var $pubdate = "";
   var $summary = "";
   var $exdate = "";
   var $story = "";

   function setHeadline($line){
      $headline = $line;
      echo $headline;
   } 
   function setUrl($line){
      $url = $line;
   }
   function setSource($line){
      $source = $line;
   }
   function setPubdate($line){
      $pubdate = $line;
   }
   function setSummary($line){
      $summary = $line;
   }
   function setExdate($line){
      $exdate = $line;
   }
   function setStory($line){
      $story = $line;
   }

   function getHeadline(){
      return $headline;
   } 
   function getUrl(){
      return $url;
   }
   function getSource(){
      return $source;
   }
   function getPubdate(){
      return $pubdate;
   }
   function getSummary(){
      return $summary;
   }
   function getExdate(){
      return $exdate;
   }
   function getStory(){
      return $story;
   }
}
?>

Stephen
Feb 6, 2004

Stoned

LightI3ulb posted:

I'm playing around with some PHP to make an RSS reader for a webpage, and I'm having a bit of trouble. If I set the function setHead to echo the input as it sets the variable, it will display, but I recieve nothing from getHeadline();

Your class variables need to be set using: $this->variable_name = whatever;
when they're being set inside the class.

LightI3ulb
Oct 28, 2006

Standard pleasure model.
Oh, I was trying $this->$variable and was getting errors.

Little Brittle
Nov 1, 2004

Come visit me dawg
What is the least resource-intensive method to grab info from an HTML page? Right now I am grabbing the whole page via CURL and finding the data I need via preg_match(), but it looks like DOMXPATH will work just as well. I'm trying to grab info on the fly from various websites, and I'd like to make it as efficient as possible. Could anyone elaborate on keeping this process lean?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Little Brittle posted:

What is the least resource-intensive method to grab info from an HTML page? Right now I am grabbing the whole page via CURL and finding the data I need via preg_match(), but it looks like DOMXPATH will work just as well. I'm trying to grab info on the fly from various websites, and I'd like to make it as efficient as possible. Could anyone elaborate on keeping this process lean?

I just open the url straight to TinyHTML to convert it into valid XHTML which I then import into SimpleXML and run an XPATH against it. If you can't turn on furl wrappers, you'll need to use curl to get the page first.

aehiilrs
Apr 1, 2007
I'm having a strange problem with PHP and APC.

I have 2 servers running CentOS 5; on one, APC compiled, installed, and ran without a problem. On the other, it gives me this during the setup:
code:
1-1, 'all', 'abort', or Enter to continue: 
and this shows up in the error log when I start apache after the install:

code:
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php/modules/apc.so' 
- /usr/lib/php/modules/apc.so: undefined symbol: php_rfc1867_callback in Unknown on line 0
Is anyone familiar with this sort of problem?

(edit for really, really long line)

Edit: Fixed. For some reason, this one had everything except for mod_php at 5.2.4, while mod_php was 5.1.6. I have no clue why.

aehiilrs fucked around with this message at 01:00 on Jul 4, 2008

Adbot
ADBOT LOVES YOU

Little Brittle
Nov 1, 2004

Come visit me dawg

duz posted:

I just open the url straight to TinyHTML to convert it into valid XHTML which I then import into SimpleXML and run an XPATH against it. If you can't turn on furl wrappers, you'll need to use curl to get the page first.
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>

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