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
Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

DirtyDaub posted:

I need some help.

Im overhauling my company's website, and i've been re-making everything from scratch so far. The only thing I don't have the knowledge to re-make is the store locator, cause its written in php (of which i have no base of understanding). So right now Im just linking the old store locator into the new site, which is fine temporarily.

The problem is, I cant figure out how to style the old locator to make it match the new site because it uses some style template written in XTPL, and I cant find any documentation about it.

So, is there someway around this? Or do I have to salvage the old data, make a new one, and teach myself php in the process?

This is the site:

old: https://www.bergfurniture.com

new: https://www.bergfurniture.com/new_site.asp

store locator: https://www.bergfurniture.com/storelocator/store_locator.php



I thought asp was pretty much dying? Either way, if I was a company I wouldn't want to move there as there are a lot more PHP devs out there.

Adbot
ADBOT LOVES YOU

LastCaress
May 8, 2004

bonobo
Is there a way to upload a picture from a remote url to my server using curl? Basically I had a firefox extension that allowed me to right click on a picture and upload it to my server, but now they turned "allow_url_fopen" off and doesn't work.

(Just need to know if there's a way, not the actual way)

Thanks.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


LastCaress posted:

Is there a way to upload a picture from a remote url to my server using curl? Basically I had a firefox extension that allowed me to right click on a picture and upload it to my server, but now they turned "allow_url_fopen" off and doesn't work.

(Just need to know if there's a way, not the actual way)

Thanks.

You mean use CURL to fetch an image? Sure, that's what it's for, to fetch things from the Internet.

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

LastCaress posted:

Is there a way to upload a picture from a remote url to my server using curl? Basically I had a firefox extension that allowed me to right click on a picture and upload it to my server, but now they turned "allow_url_fopen" off and doesn't work.

(Just need to know if there's a way, not the actual way)

Thanks.

Here is a very basic example:
Please read the curl documentation
php:
<?
$url = "http://www.domain.com/image.jpg";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$fileContents = curl_exec($ch);
curl_close($ch);
$newImg = imagecreatefromstring($fileContents);
?>

LastCaress
May 8, 2004

bonobo
Thanks, I'm a veterinarian and obviously know nothing about coding, so before I would spend a lot of time trying to figure that out I just wanted to know if it worked :)

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

LastCaress posted:

Thanks, I'm a veterinarian and obviously know nothing about coding, so before I would spend a lot of time trying to figure that out I just wanted to know if it worked :)

It's okay, just make sure to tell us how to fix our animals when we need it.

Sterf
Dec 31, 2004

Another complete coding-newbie question here:

How do you replace php's copy() with curl?

I've got a wordpress script that uses the copy function to download a file to my website, and allow_url_fopen or something is recently been disabled by my host so it doesn't work anymore.

This is the relevant piece of code (I think):

code:
$filename = WP_PLUGIN_DIR."/mochi/mochigames/".$game[blog_id].".swf";
$copy = copy($game[swf_url],$filename);
if($copy){
$query = "update ".$wpdb->prefix."mochi set blog_url = \"$filename\" where id = $game[id]";
$wpdb->query($query);
Any help would be really appreciated. I somehow fixed the script to download a feed, but I can't get this to work. I know nothing of coding, and I broke my site a hundred times already.

Amethyst
Mar 28, 2004

I CANNOT HELP BUT MAKE THE DCSS THREAD A FETID SWAMP OF UNFUN POSTING
plz notice me trunk-senpai
Hey guys, I'm helping my GF write her honours thesis with a simple php scraper of a vBulletin board (not SA).

I'm not particularly proficient with php and an just making the very first steps. What I need help with is how to get php to "log in" so it can view content available to members. Is there a way to load cookies into php before using file_get_contents($url)?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Amethyst posted:

Hey guys, I'm helping my GF write her honours thesis with a simple php scraper of a vBulletin board (not SA).

I'm not particularly proficient with php and an just making the very first steps. What I need help with is how to get php to "log in" so it can view content available to members. Is there a way to load cookies into php before using file_get_contents($url)?

http://php.net/manual/en/function.file-get-contents.php

Crazy what they put in the documentation these days. Example 4.

nullfox
Aug 19, 2008

Amethyst posted:

Hey guys, I'm helping my GF write her honours thesis with a simple php scraper of a vBulletin board (not SA).

I'm not particularly proficient with php and an just making the very first steps. What I need help with is how to get php to "log in" so it can view content available to members. Is there a way to load cookies into php before using file_get_contents($url)?

Use cURL - With cURL you can login and let it manage the cookies for you.

I don't have a complete example, but this should give you enough idea and get you started: http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/

Ninja Edit: Is there any reason your using file_get_contents() vs cURL anyway? In 99% of instances I would recommend people use cURL as their first step as it isnt hamstrung by safe mode or url open stuff, and it is far more flexible then other options.

nullfox
Aug 19, 2008

Sterf posted:

Another complete coding-newbie question here:

How do you replace php's copy() with curl?

I've got a wordpress script that uses the copy function to download a file to my website, and allow_url_fopen or something is recently been disabled by my host so it doesn't work anymore.

This is the relevant piece of code (I think):

code:
$filename = WP_PLUGIN_DIR."/mochi/mochigames/".$game[blog_id].".swf";
$copy = copy($game[swf_url],$filename);
if($copy){
$query = "update ".$wpdb->prefix."mochi set blog_url = \"$filename\" where id = $game[id]";
$wpdb->query($query);
Any help would be really appreciated. I somehow fixed the script to download a feed, but I can't get this to work. I know nothing of coding, and I broke my site a hundred times already.

This link has a decent example of what your trying to do: http://www.weberdev.com/get_example-4009.html

Amethyst
Mar 28, 2004

I CANNOT HELP BUT MAKE THE DCSS THREAD A FETID SWAMP OF UNFUN POSTING
plz notice me trunk-senpai

Lumpy posted:

http://php.net/manual/en/function.file-get-contents.php

Crazy what they put in the documentation these days. Example 4.

WildFoxMedia posted:

Use cURL - With cURL you can login and let it manage the cookies for you.

I don't have a complete example, but this should give you enough idea and get you started: http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/

Ninja Edit: Is there any reason your using file_get_contents() vs cURL anyway? In 99% of instances I would recommend people use cURL as their first step as it isnt hamstrung by safe mode or url open stuff, and it is far more flexible then other options.

Thanks for the help, I know it was basic but I just needed a push in the right direction :)

nullfox
Aug 19, 2008
Yarp - No problem

Sorry if my last comment seemed abrasive - Among the many things that "irk" me about PHP, things like file_get_contents and copy via remote URL open bother me most since they tend to have issues and are subject to safe mode and other oddball restrictions.

DirtyDaub
Jul 26, 2007
Keeping it real for generations at a time

haywire posted:

I thought asp was pretty much dying? Either way, if I was a company I wouldn't want to move there as there are a lot more PHP devs out there.

Probably, I dont really see it anywhere. The only reason I'm using it is cause the old site was built with it and its the only way i know how to use:

<!-- include file -->

I use this for the header and footer so I dont have to change them out on each page. Theres probably a better way to do this right?

Supervillin
Feb 6, 2005

Pillbug

DirtyDaub posted:

Probably, I dont really see it anywhere. The only reason I'm using it is cause the old site was built with it and its the only way i know how to use:

<!-- include file -->

I use this for the header and footer so I dont have to change them out on each page. Theres probably a better way to do this right?

Same thing:

<?php include('filename'); ?>

DirtyDaub
Jul 26, 2007
Keeping it real for generations at a time
Cool, I'll start using that.

Sterf
Dec 31, 2004

WildFoxMedia posted:

This link has a decent example of what your trying to do: http://www.weberdev.com/get_example-4009.html

I tried applying that, but it gives an error.
Fatal error: Call to undefined function get_file1()

Is it something I did wrong or is something disabled on the server or something? (remember, complete newbie here)

This is what I made of it. I probably butchered it, but yeah...
code:
$filename = WP_PLUGIN_DIR."/mochi/mochigames/".$game[blog_id].".swf";
$filefolder_local = WP_PLUGIN_DIR."/mochi/mochigames/";
$filename_local = $game[blog_id].".swf";
 
$copy = get_file1($game[swf_url], $filefolder_local, $filename_local) ;
 $ch = curl_init();
    curl_setopt($ch, CURLOPT_FILE, $out);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_exec($ch);
    echo "<br>Error is : ".curl_error ( $ch);
    curl_close($ch); 
	
if($copy){
$query = "update ".$wpdb->prefix."mochi set blog_url = \"$filename\" where id = $game[id]";
$wpdb->query($query);

SuckerPunched
Dec 20, 2006

Did you add the function get_file1 to your code, that was listed on the page?

php:
<?php
function get_file1($file$local_path$newfilename)
{
    $err_msg '';
    echo "<br>Attempting message download for $file<br>";
    $out fopen($newfilename'wb');
    if ($out == FALSE){
      print "File not opened<br>";
      exit;
    }
   
    $ch curl_init();
           
    curl_setopt($chCURLOPT_FILE$out);
    curl_setopt($chCURLOPT_HEADER0);
    curl_setopt($chCURLOPT_URL$file);
               
    curl_exec($ch);
    echo "<br>Error is : ".curl_error $ch);
   
    curl_close($ch);
    //fclose($handle);

}//end function
?>

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



SuckerPunched posted:

Did you add the function get_file1 to your code, that was listed on the page?

He just copied and pasted in the interesting bits.

Here:
php:
<?
function curl_download($remoteURL, $toFilename){
    if(($fh = fopen($toFilename, 'wb')) === false){
        echo "Unable to open '$toFilename' in write mode!";
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remoteURL);
    curl_setopt($ch, CURLOPT_FILE, $fh);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    if(curl_errno($ch)){
        echo "Error downloading '$remoteURL': ", curl_error($ch);
        return false;
    }
    curl_close($ch)
    return true;
}

$filename = WP_PLUGIN_DIR."/mochi/mochigames/".$game[blog_id].".swf";
$filefolder_local = WP_PLUGIN_DIR."/mochi/mochigames/";
$filename_local = $game[blog_id].".swf";

//no idea where remoteURL is coming from (assuming filename is the local path)!
if(curl_download($remoteURL, $filename)){//you get to figure that out
    $query = "update ".$wpdb->prefix."mochi set blog_url = \"$filename\" where id = $game[id]";
    $wpdb->query($query);
}
?>
Try that. edit: now tested, seems to work

Munkeymon fucked around with this message at 20:44 on Dec 16, 2009

Sterf
Dec 31, 2004

SuckerPunched posted:

Did you add the function get_file1 to your code, that was listed on the page?

Oops, apparantly I didn't. And what I failed to mention is that the code I showed is already in one of those 'function' brackets. I'm guessing by the error it gives me you can't put one of those inside another? Here's the complete code thingie excerpt btw. The code I showed previously was just the one in the 'else':

php:
<?
php
function mochi_toggle_games($id){
     global $wpdb;
     $id = intval($id);    
     $query = "select id,swf_url,blog_url,blog_id from ".$wpdb->prefix."mochi where id = $id";
     $game = $wpdb->get_row($query,ARRAY_A);
     if($game[blog_url]){
          $unlink = unlink($game[blog_url]);
          if($unlink){
               $query = "update ".$wpdb->prefix."mochi set blog_url = \"\" where id = $game[id]";
               $wpdb->query($query);
          }     
     }
     else{
          $filename = WP_PLUGIN_DIR."/mochi/mochigames/".$game[blog_id].".swf";
          $copy = copy($game[swf_url],$filename);
          if($copy){
               $query = "update ".$wpdb->prefix."mochi set blog_url = \"$filename\" where id = $game[id]";
               $wpdb->query($query);
          }   
     }   
}?>
I have no idea where to put this new function, or, well, everything else.
Sorry for probably being an idiot, but this is all chinese for me (I'm not chinese). It's all a bit above my head, so I'll probably just have to give up for now and first try and learn basic php. Thanks for helping, though.

edit:

quote:

He just copied and pasted in the interesting bits.
I'm afraid that's my way of working with this so far. I kind of feel like an 80 year old using a computer for the first time. And thanks for your help, I'll try and decipher it with my '80 year old first time computer user' goggles.
I shall now flee in shame.

Sterf fucked around with this message at 20:50 on Dec 16, 2009

hallik
Aug 15, 2003
I have a class with a bunch of methods, and comments in front of each method. Is there a way to grab all those comments in a array or something so I can echo them in an API page?

<edit> Just had a friend tell me, use ReflectionClass which is working great. nevermind. :)

hallik fucked around with this message at 04:38 on Dec 17, 2009

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

How would I go about making sure something happens 75% or n% of the time?

If that didn't make sense, how can I make a random event happen n% of the time whenever a page is loaded.

I tried this but this doesn't seem right.

php:
<?
$r = rand(1,100);
if($r <= 75)
    echo "hit";
else
    echo "miss";?>
Thoughts?

spiritual bypass
Feb 19, 2008

Grimey Drawer

hallik posted:

I have a class with a bunch of methods, and comments in front of each method. Is there a way to grab all those comments in a array or something so I can echo them in an API page?

<edit> Just had a friend tell me, use ReflectionClass which is working great. nevermind. :)

Are you writing actual PHPdoc? If not, it's easy to do and you should start doing it.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

drcru posted:

How would I go about making sure something happens 75% or n% of the time?

If that didn't make sense, how can I make a random event happen n% of the time whenever a page is loaded.

I tried this but this doesn't seem right.

php:
<?
$r = rand(1,100);
if($r <= 75)
    echo "hit";
else
    echo "miss";?>
Thoughts?

Did you initialize the random number generator at some point? [EDIT] looks like you don't need to do this since 4.2, but I dunno what version you are on....

php:
<?
srand( time() );
$num = rand(1,100);
printf("I generated %d", $num);
if( $num <= 75 )
{
 // yippie!
}else
{
 // boo
}
?>

Lumpy fucked around with this message at 07:51 on Dec 17, 2009

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

drcru posted:

How would I go about making sure something happens 75% or n% of the time?

If that didn't make sense, how can I make a random event happen n% of the time whenever a page is loaded.

I tried this but this doesn't seem right.

php:
<?
$r = rand(1,100);
if($r <= 75)
    echo "hit";
else
    echo "miss";?>
Thoughts?

What do you mean by "doesn't seem right"? It looks like it should work.

You could check whether it really is generating numbers like you want by temporarily changing it to the following

php:
<?
$hits = 0;
$misses = 0;
for ($i=0;$i<10000;$i++) {
    $r = rand(1,100);
    if ($r <= 75) { $hits++;   }
    else          { $misses++; }
}
echo $hits.' hits and '.$misses.' misses.';
?>
Then you should see a result that looks something like "7579 hits and 2421 misses."

hallik
Aug 15, 2003

rt4 posted:

Are you writing actual PHPdoc? If not, it's easy to do and you should start doing it.

I am trying to make it easy for a non php person at work to know what classes/methods are available to him, and how to call them from his application. I will google PHPDoc and see what it's all about.

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

hallik posted:

I am trying to make it easy for a non php person at work to know what classes/methods are available to him, and how to call them from his application. I will google PHPDoc and see what it's all about.

PHPDoc is perfect for that - not only is it useful when reading the source itself, but it generates an HTML (or whatever else) "help file" that sort of documents your code as per what it can find itself and what you've written.

gwar3k1
Jan 10, 2005

Someday soon
It's been a long time since I last wrote session scripts and user authorization, so I want to check what I'm doing isn't pointless.

I've used the PHP Security Consortium example of sessions to write a three-tiered validation (session active > session user agent (hashed) > url user agent (hashed)). If I set a boolean to true if the validation passes, can I stick an if outside of the session code to call user functions?

I.e. if($uservalid == true) { echo userInput(); }

Does that destroy the point of checking the session in the first place?

My second question (if the answer to the first is positive), can I abstract the session validation to a separate file and just call it like I would any other function?

php:
<?php
  session_start();
  validate_session();
?>
Basically, I'm unsure if session checking has to be integral to all pages and if the code you want to run if the session is valid has to be present within the validation code. Years ago, I would just write my required code and put it within the session=valid area of the validation code. Now I'm a bit more clued up on the benefits of abstraction, I'd like to know if I can abstract validation too.

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums
Surely if your session is invalid you should most likely kill the script and tell the client to gently caress off, whilst deleting the session? Or just do the latter.

gwar3k1
Jan 10, 2005

Someday soon
I was thinking of having update forms on certain pages which were only visible to users that are logged in. That way they all existing content is visible. If the session is invalid, the page will still load, but without the form.

Is this bad practice?

MrMoo
Sep 14, 2000

haywire posted:

Surely if your session is invalid you should most likely kill the script and tell the client to gently caress off, whilst deleting the session? Or just do the latter.

Just re-initialize the session and redirect to the homepage.

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

MrMoo posted:

Just re-initialize the session and redirect to the homepage.

Essentially ending the script.

CoderCat
May 7, 2005

Science, it works. :science:

gwar3k1 posted:

I was thinking of having update forms on certain pages which were only visible to users that are logged in. That way they all existing content is visible. If the session is invalid, the page will still load, but without the form.

Is this bad practice?

Make sure you also protect the script that processes the form with the same checks. Otherwise, users who had access to the form structure at some point (or just guessed it) would be able to submit forms without logging in.

Mr Viper
Jun 21, 2005

Derezzed...
Is there some sort of way to send multiple mail() queries at once? I set up an "Email All Users" thing for my site, and it takes 2 seconds a mail(), and there are 1500 users. You can see how this is a problem. What can I do to speed the process up?

Supervillin
Feb 6, 2005

Pillbug

Mr Viper posted:

Is there some sort of way to send multiple mail() queries at once? I set up an "Email All Users" thing for my site, and it takes 2 seconds a mail(), and there are 1500 users. You can see how this is a problem. What can I do to speed the process up?

Are the mail subjects/bodies identical? mail() accepts virtually any number of receivers per call (Example 4, delimit with commas). If each mail has to be tailored to its recipient, you probably have to send them one at a time.

The documentation also points to PEAR for bulk sending: http://pear.php.net/package/Mail and http://pear.php.net/package/Mail_Queue.

Mr Viper
Jun 21, 2005

Derezzed...

Supervillin posted:

Are the mail subjects/bodies identical? mail() accepts virtually any number of receivers per call (Example 4, delimit with commas). If each mail has to be tailored to its recipient, you probably have to send them one at a time.

The documentation also points to PEAR for bulk sending: http://pear.php.net/package/Mail and http://pear.php.net/package/Mail_Queue.

I gave the multiple "to"s method a shot, and it seems to still take the standard 2 seconds per email... And I don't feel like dealing with PEAR.

MrMoo
Sep 14, 2000

Mr Viper posted:

it takes 2 seconds a mail()

Sounds like you are not queuing mail in the MTA, or the MTA is remote to the script host. You need to configure a local MTA to spool out for later delivery.

Rat Supremacy
Jul 15, 2007

The custom title is an image and/or line of text that appears below your name in the forums

CoderCat posted:

Make sure you also protect the script that processes the form with the same checks. Otherwise, users who had access to the form structure at some point (or just guessed it) would be able to submit forms without logging in.


Surely this is security by obscurity? The user could make anything they like with an HTTP request, it is up to your script to ignore the submitted form if there's no way to authenticate it. So, in theory, you could give them every piece of information about the drat form, but no matter how they submit it, unless they are logged in it wont work.

CoderCat
May 7, 2005

Science, it works. :science:

haywire posted:

Surely this is security by obscurity? The user could make anything they like with an HTTP request, it is up to your script to ignore the submitted form if there's no way to authenticate it. So, in theory, you could give them every piece of information about the drat form, but no matter how they submit it, unless they are logged in it wont work.

Exactly, this is what I'm trying to say. The script that processes the form should also be protected. I've seen many pieces of code in which the form is protected but the processing code has no protection.

Adbot
ADBOT LOVES YOU

gwar3k1
Jan 10, 2005

Someday soon
So I should be encapsulating my "action" scripts with the session authorization and not just setting a variable that says the user is logged in and the session is valid? That makes sense.

So this...
code:
is session
  is same user agent
    is fingerprint
      code
else
  gently caress off
not this...
code:
auth = false
is session
  is same user agent
    is fingerprint
      auth = true
else
  gently caress off
-------------
if auth
  code
else
  do nothing

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