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
<deleted user>

Grigori Rasputin posted:

Since zero evalutes to false, it fails even though I can tell the difference between a month that never occurs and a month at the zero position - I just can't test for it.

You can test specifically for failure. This will set $i to zero, but the echo will not happen.

php:
<?
$str = 'january is my favorite month';
if(($i = strpos($str, 'january')) === false) echo "not found\n";
?>

Adbot
ADBOT LOVES YOU

Zorilla
Mar 23, 2005

GOING APE SPIT
Triple-equals signs always weird me out. Since we're evaluating the success/failure of a variable assignment, is your example the same as this?

php:
<?
$str = 'january is my favorite month';
if (!($i = strpos($str, 'january'))) echo "not found\n";
?>

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Zorilla posted:

Triple-equals signs always weird me out. Since we're evaluating the success/failure of a variable assignment, is your example the same as this?

php:
<?
$str = 'january is my favorite month';
if (!($i = strpos($str, 'january'))) echo "not found\n";
?>

No, because if you are at position 0, it can be evaluated as false, and your expression will return true, and you will see "not found"

Learn to love triple equals and it's pal !==

<deleted user>

Zorilla posted:

Triple-equals signs always weird me out. Since we're evaluating the success/failure of a variable assignment, is your example the same as this?

php:
<?
$str = 'january is my favorite month';
if (!($i = strpos($str, 'january'))) echo "not found\n";
?>

It's not the same, because strpos() will find the string successfully and return zero (because the match occurs at offset zero). It's a case where a non-true value does not indicate failure.

The triple equals does the same comparison, but only succeeds if the values compared are of the same type. The false keyword is implicitly boolean, so it is like (bool)0. This lets you check if strpos() failed regardless of its return value.

LastCaress
May 8, 2004

bonobo
So, how would I go about "renting" a php coder? I had a very good idea for my game, but it's way over my head (even if it's not that complicated). I appreciate all the help I've been given but I don't want to be constantly bothering people about every detail.

EDIT : put a bid on rentacoder but don't know if anyone will be interested, it's very small, under $35 dollars :|

LastCaress fucked around with this message at 12:04 on Apr 23, 2008

noonches
Dec 5, 2005

It represents my lifestyle and stratus as a street savy irreverent youth, who lives large, and yet hungers for the next level in life.
Are there a limitations to file_get_contents() that I am not aware of? Namely it not accepting urls that are really long (349 characters)?


Ed: got it, a space hidden within a single field in the long rear end url.

noonches fucked around with this message at 16:18 on Apr 23, 2008

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO

Zorilla posted:

PHP has powerful functionality for manipulating date and time formats. What is the end goal of this code?

I'm parsing an event calender and putting it into the database. The original format is not completely conducive to being machine-readable but I've got a pretty effective strategy in place for parsing it... one day I'm just going to have to buckle and really learn REGEX though.

As far as the month bit, I strip out all the date info and pass it to mktime() and then pass the raw time off to the DB. The dates can be in strange formats for multi-date events so I kinda have to make due. If you have any recommendations for date functions outside of date() and mktime() I'd wouldn't mind hearing/reading more about them though.

Everyone else: thanks for the === info, I guess I need to read up on operators a little more.

So far I really like PHP, after programming in PL/SQL and object-oriented languages for the past few years it's refreshingly powerful without a lot of the excess of something like Java, where it seems like you spend 99% of your time MAKING EVERYTHING A CLASS (which I really enjoy too). Being able to use PEAR and to connect to a DB, query it and spit out the results in under ten lines rules, it's so straightforward. It is really easy to make mistakes though, I'll forget my $ on variables every so often or gently caress up variable case.

Any good and simple IDEs that the detect errors? I'm using NotePad++ which is good, but don't mind a few frills like method sensing and whatnot.

noonches
Dec 5, 2005

It represents my lifestyle and stratus as a street savy irreverent youth, who lives large, and yet hungers for the next level in life.

Grigori Rasputin posted:

If you have any recommendations for date functions outside of date() and mktime() I'd wouldn't mind hearing/reading more about them though.

strtotime() is an amazingly powerful little function.

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO
Oh, another quick one that's more of a general *AMP/database question. Should I store timestamps in the format generated by PHP's mkdate() or MySQL's native date format?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Grigori Rasputin posted:

Oh, another quick one that's more of a general *AMP/database question. Should I store timestamps in the format generated by PHP's mkdate() or MySQL's native date format?

Storing them as the databases' native date format has the advantage that you know it's a date because it says so and not because the program uses it as one.

noonches
Dec 5, 2005

It represents my lifestyle and stratus as a street savy irreverent youth, who lives large, and yet hungers for the next level in life.

duz posted:

Storing them as the databases' native date format has the advantage that you know it's a date because it says so and not because the program uses it as one.

Not to mention MySQL's built in date functions that won't work on something that is not a date.

other people
Jun 27, 2004
Associate Christ
I have a general question about arrays and objects. I have been using code igniter a lot lately and it is (in most cases) happy to accept an object as a function input anywhere that it will take an array.

I think my code is a bit easier to read using objects instead of arrays. The syntax of arrays always seemed a bit clunky to me.

There are a few places where CI will not take an object and only works with arrays so I have had to break out the odd array here and there throughout my code. It has me thinking now about arrays and objects and if I should be using one over the other.

I realize objects can hold more data types but I have never built my own object/class. I don't have any class functions inside my objects. I tried in CI once and failed miserably. I don't know if I was doing it completely wrong or if I wasn't following CI's specifications.

So my objects just look like arrays for the most part. In fact, anywhere that I was forced to use an array I always change the variable to an object type as soon as I can to continue working with it.

Should I be using objects in this manner? I am just using them to store string variables which I pass between functions for the most part. I just think it looks nicer when you define them, and especially when you have to echo them out into your final html.

Begby
Apr 7, 2005

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

Kaluza-Klein posted:

I have a general question about arrays and objects. I have been using code igniter a lot lately and it is (in most cases) happy to accept an object as a function input anywhere that it will take an array.

I think my code is a bit easier to read using objects instead of arrays. The syntax of arrays always seemed a bit clunky to me.

There are a few places where CI will not take an object and only works with arrays so I have had to break out the odd array here and there throughout my code. It has me thinking now about arrays and objects and if I should be using one over the other.

I realize objects can hold more data types but I have never built my own object/class. I don't have any class functions inside my objects. I tried in CI once and failed miserably. I don't know if I was doing it completely wrong or if I wasn't following CI's specifications.

So my objects just look like arrays for the most part. In fact, anywhere that I was forced to use an array I always change the variable to an object type as soon as I can to continue working with it.

Should I be using objects in this manner? I am just using them to store string variables which I pass between functions for the most part. I just think it looks nicer when you define them, and especially when you have to echo them out into your final html.

Using objects can help immensely. So you are on the right track, but could get your learn on in the realm of OO.

Here is an example of a simple object and how it might be useful.

code:

class Person
{
  public $birthYear;
  public $firstName;
  public $lastName;

  public function Age()
  {
     return date('Y', time())  - $this->birthYear;
  }  

}

$me = new Person();
$me->birthYear = 1976;
echo $me->Age();
The above is pretty simple. You have an object that represents a person, but we added something that calculates an age. I think you can probably figure out the usefulness.

Anyways, there are a billion books out there on OO theory. You don't have to get one that is based on PHP, you can read one that is about Java or even C++ and as long as you understand the theory you can apply it to PHP.

As far as CI is concerned, you can make an object behave more like an array in php by having it implement some interfaces (there are also magic methods defined here http://www.php.net/manual/en/language.oop5.magic.php)

Here is a simple example. You would probably want to use the __get() and __set() magic methods to make it a little more solid.

code:

// The ArrayAccess interface defines 4 methods that you implement, this lets you use your
// object with array-like bracket syntax
class Person implements ArrayAccess
{
  public $firstName = "";
  public $lastName = "";

  public function offsetExists ($offset)
  {
    if isset($this->$offset) return true ;
    throw new Exception ('offset undefined');
  }

  public function offsetGet ($offset)
  {
    if isset($this->$offset) return $this->$offset;
    throw new Exception ('oh crap');
  }

  public function offsetSet ($offset, $value)
  {
    if isset($this->$offset) $this->$offset = $value;
    throw new Exception ('oh crap');
  }

  public function offsetUnset ($offset)
  {
   etc.
  }
}

$me = new Person() ;
$me['firstName'] = 'Fred'; // This calls the offsetSet method and passes it 'firstName' for $offset and 'Fred' for $value
$me->firstName = 'Fred'; // bypasses the offsetSet method, but
$me['food'] = 'taco'; //throws an exception

There are lots of things you can do with objects in PHP. The above may not solve your CI solution, it depends on what the functions are doing with the array you pass it, but there are other interfaces such as GetIterator that may solve your problem. You can look them up here http://www.php.net/~helly/php/ext/spl/


Before you do any of that though, brush up on some theory.

sc0tty
Jan 8, 2005

too kewell for school..
I've just started to look into PHP and I don't have a whole lot of programming experience, just some basic Java background. I'm sure there is a basic solution, so here it goes.

I have a $_SESSION['array'] with some basic data in it, and I need to remove one of the entry's completely, and effectively shift everything in the array up to replace it. How would I go about doing this?

I haven't provided a lot of detail as I'm sure there is a simple solution to this, however if I have done a lovely job of explaining my problem then I can provide some more of my code and go into it in more detail.

Evil Angry Cat
Nov 20, 2004

sc0tty posted:

I've just started to look into PHP and I don't have a whole lot of programming experience, just some basic Java background. I'm sure there is a basic solution, so here it goes.

I have a $_SESSION['array'] with some basic data in it, and I need to remove one of the entry's completely, and effectively shift everything in the array up to replace it. How would I go about doing this?

I haven't provided a lot of detail as I'm sure there is a simple solution to this, however if I have done a lovely job of explaining my problem then I can provide some more of my code and go into it in more detail.

You want array_values()

php:
<?
$my_array = array (0 => 'Mike', 1 => 'Rob', 2 => 'Jeff', 3 => 'Tom');

//do whatever with $my_array[1] and then remove it from the array with unset($my_array[1])

$my_array = array_values($my_array);

//Produces array = ([0] => Mike, 1 => Jeff, 2 => Tom);
?>
You don't need to put the 0 => in when creating an array thats just for example purposes.

sc0tty
Jan 8, 2005

too kewell for school..

Evil Angry Cat posted:

You want array_values()

php:
<?
$my_array = array (0 => 'Mike', 1 => 'Rob', 2 => 'Jeff', 3 => 'Tom');

//do whatever with $my_array[1] and then remove it from the array with unset($my_array[1])

$my_array = array_values($my_array);

//Produces array = ([0] => Mike, 1 => Jeff, 2 => Tom);
?>
You don't need to put the 0 => in when creating an array thats just for example purposes.

Thanks this is exactly what I was looking for.

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO
Got a strange problem I figure you guys can take a crack at.

I'm making a real basic file upload utility that uploads an access db that's data is to be imported into a mysql db. I've got the upload utility working for small (< 1MB) files, but when I test in the 5-30MB range nothing happens after post - I simply get a white page with none of my print/echo statements rendering, no errors, no warnings. Anyone have any clues?

I have a hidden field for MAX_FILE_SIZE in my form that's plenty large for files of this size as well as upload_max_filesize set to 30MB in php.ini.

Is there something I'm overlooking, is this a php bug, what's the deal? I know it has to be possible to upload 30MB files no problem.

edit: After prodding it a bit more, it seems as though the POST never even occurs...\

edit 2: I'm a PHP WIZARD!!!!! Solution:

Increase post_max_size in php.ini

Grigori Rasputin fucked around with this message at 20:28 on Apr 28, 2008

Land Mime
Jul 13, 2007
Love never blows up and gets killed
Make sure post_max_size in your php.ini is also set appropriately.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Don't forget post_max_size as well as any database size limits.

Zorilla
Mar 23, 2005

GOING APE SPIT
Or better yet, put something like this in your .htaccess so you don't change PHP settings globally:

code:
php_value memory_limit 34M
php_value post_max_size 33M
php_value upload_max_filesize 32M
php_value max_execution_time 600
I ended up using this method for our project management site because the host's 2MB default post limit was really starting to get in the way.

Alternatively, you can adjust php.ini settings during the run of a script with ini_set();

other people
Jun 27, 2004
Associate Christ

Begby posted:

Using objects can help immensely. So you are on the right track, but could get your learn on in the realm of OO.

There are lots of things you can do with objects in PHP. The above may not solve your CI solution, it depends on what the functions are doing with the array you pass it, but there are other interfaces such as GetIterator that may solve your problem. You can look them up here http://www.php.net/~helly/php/ext/spl/


Before you do any of that though, brush up on some theory.

Woah, thank you Begby. That was very helpful!

I gave objects another shot and I have my first CI library up and running!

I am now on my second library and I have hit a snag. I tried to do this:
php:
<?
class Listing {

       public $default->display_type = 'all';
       public $default->display_order = 'title';

       public function read_uri() {
              $uri_options = $this->ci->uri->uri_to_assoc(3, $default);

              bla bla bla

       }

}
?>
I have a __constructor() function that loads the Code Igniter stuff. That uri_to_assoc() function expects an object as the 2nd param ($default). My problem is that apparently you cannot set objects like that at the start of the class. It throws a fit at the first line inside the class public $default->display_type = 'all';. If I just set $display_type and $display_order it is happy, but then I have to put them into an object inside read_uri() (I guess).

I imagine there is a good reason I cannot set an object like that at the start? How should I do it instead? Makes me wonder if I can set an array. . .



edit: I realize now why having an object declared like that is a bad idea. You could declare a variable $what and then also have a function called $what and that would not be good. You can declare arrays, but only if you use the syntax $bla = array ('huh'), doing $bla[] = 'huh' doesn't work.


Now I am a bit confused as to what to keep inside the class and what functions should be by themselves!

other people fucked around with this message at 05:25 on May 1, 2008

nbv4
Aug 21, 2002

by Duchess Gummybuns

Kaluza-Klein posted:


I have a __constructor() function that loads the Code Igniter stuff. That uri_to_assoc() function expects an object as the 2nd param ($default). My problem is that apparently you cannot set objects like that at the start of the class. It throws a fit at the first line inside the class public $default->display_type = 'all';. If I just set $display_type and $display_order it is happy, but then I have to put them into an object inside read_uri() (I guess).

I imagine there is a good reason I cannot set an object like that at the start? How should I do it instead? Makes me wonder if I can set an array. . .

I don't know if this is related, but you can't call functions from within a class variable declaration. For instance:

php:
<?

class poop extends woop
{
   var variable1 = "4389348";
   var variable2 = "fdjf545478";
   var variable3 = trim("fdsuiofd ui fui fds ");

function poop()
{
    print "poo lol";
}
?>
Will give you an error because you called trim(). I have no idea why this is the case, but you just can't do that :shobon:

quote:

edit: I realize now why having an object declared like that is a bad idea. You could declare a variable $what and then also have a function called $what and that would not be good. You can declare arrays, but only if you use the syntax $bla = array ('huh'), doing $bla[] = 'huh' doesn't work.

When you do $blah[] = "something", the "=" operator kind of works like the ".=" operator. Your basically saying "add this value to the array", When the array has not been set, an error occurs.

duck monster
Dec 15, 2004

Back a whole bunch of pages was the question "How to stop image leeching".

Do this;- Compress 1 gig of zeros into a file named "image.jpg.gz". It'll come down to a megabyte.

Use url redirect and some magic to serve that as a gzip encoded http stream instead when the referer is all wrong.

Being that this satanic son of a bitch memory-murders anything that tries to leech it, the leeching stops very loving quickly.

:eng101: This is also a good way to massacre bots that ignore your robots.txt

put in something like dont_download_me_or_yourbrowser_will_die.html which is actually the browser bomb in your robots, and watch as spam spiders die screaming (some, not all, some will use smarter stream tech and not die.)

duck monster fucked around with this message at 08:27 on May 1, 2008

Inquisitus
Aug 4, 2006

I have a large barge with a radio antenna on it.

duck monster posted:

Back a whole bunch of pages was the question "How to stop image leeching".

Do this;- Compress 1 gig of zeros into a file named "image.jpg.gz". It'll come down to a megabyte.

Use url redirect and some magic to serve that as a gzip encoded http stream instead when the referer is all wrong.

Being that this satanic son of a bitch memory-murders anything that tries to leech it, the leeching stops very loving quickly.

:eng101: This is also a good way to massacre bots that ignore your robots.txt

put in something like dont_download_me_or_yourbrowser_will_die.html which is actually the browser bomb in your robots, and watch as spam spiders die screaming (some, not all, some will use smarter stream tech and not die.)

I like this idea :clint:

Zorilla
Mar 23, 2005

GOING APE SPIT
It's like some sort of standards compliant tall.gif

duck monster
Dec 15, 2004

Its a straight out rip of a technique people used to use to rape virus checkers , and particularly mail based ones, many moons ago.

Exchange (specifically) based virus checkers used to open up mails and examine them in-situ. The idea of the attack was to exploit the virus checkers ability to open zip files and examined them for virus's. So the trick was to get a gig or so of zeros and compress them to , like , a one meg file, and send it to the server. The server would then take it, open the zip file, and malloc() like an insane bitch to make memory for it, thus rendering itself completely gibberingly brain-dead. Eventually virus checkers got smart and switched to streams based zip processing.

Thing is, this attack vector STILL works against browsers and MANY spiders. You basically tell apache to take the gzip file and under NO circumstance open it for sending (because you'll end up raping your own box) , but just send it as the pre gzipped html page.

When I first tested it against a known spider, I noticed it accepted gzip html, so I tried it out. As the spider hit, I started pinging it, and watched as it fell for the trap. Very quickly the pings slowed down until after about 2 minutes, it just dropped off. Presumably the spider went catatonic.

Its nasty, its brutal, and it serves weblog spammer cunts right.

LP0 ON FIRE
Jan 25, 2006

beep boop
I was using php code to check if a sent $_GET didn't come from outside of my website. Now I want to do the same thing with $_POST, and I expected the code to work for it, but it doesn't do anything inside the if statement if a $_POST occurred. I tested to make sure this was the case by echoing the sent $_POST outside of the if statement, which works.

Here's the code I used to validate a $_GET:

code:
<?php
if (strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com') !== false){
echo $_POST["sentVar"];
}
?>

LP0 ON FIRE fucked around with this message at 22:19 on May 2, 2008

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

awdio posted:

I was using php code to check if a sent $_GET didn't come from outside of my website. Now I want to do the same thing with $_POST, and I expected the code to work for it, but it doesn't do anything inside the if statement if a $_POST occurred. I tested to make sure this was the case by echoing the sent $_POST outside of the if statement, which works.

Here's the code I used to validate a $_GET:

code:
<?php
if (strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com') !== false){
echo $_POST["sentVar"];
}
?>

Hmm, I'm stumped on that one, but as a quick check, try this:

php:
<?php
$tmp strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com');
echo "My test returned: ".$tmp;
if ($tmp !== false){
echo $_POST["sentVar"];
}else{
echo "Hey, my test failed! ".$_POST["sentVar"];
}
?>

Just to be 100% sure your test is correct, but by the looks of it it should be.

LP0 ON FIRE
Jan 25, 2006

beep boop
Lumpy I tried your test and it failed.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

awdio posted:

Here's the code I used to validate a $_GET:

code:
<?php
if (strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com') !== false){
echo $_POST["sentVar"];
}
?>

I thought HTTP_REFERER couldn't be trusted?

Zorilla
Mar 23, 2005

GOING APE SPIT

awdio posted:

code:
<?php
if (strpos(strtolower($_SERVER['HTTP_REFERER']), 'mywebsite.com') !== false){
echo $_POST["sentVar"];
}
?>

Shouldn't referrer handling be done though .htaccess?

Standish
May 21, 2001

fletcher posted:

I thought HTTP_REFERER couldn't be trusted?
Cross-site request forgery isn't an attack on the server, it's an attack on the client. It doesn't enable the client to do anything it doesn't already have permission to do, it just tricks the client into doing it with the wrong parameters.

Standish fucked around with this message at 00:16 on May 3, 2008

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Zorilla posted:

Shouldn't referrer handling be done though .htaccess?

Ideally.

LP0 ON FIRE
Jan 25, 2006

beep boop
I figured out the HTTP_REFERER issue. I don't know if this was obvious to any of you, but when you use $_POST as opposed to $_GET, you actually have to specify the ENTIRE address, not just the file name even if it is in the same directory to be able to send the HTTP_REFERER information and get it back with $_POST. Of course the information for the $_POST will return, but not the HTTP_REFERER.

LP0 ON FIRE
Jan 25, 2006

beep boop
edit sorry wrong thread.

LP0 ON FIRE fucked around with this message at 18:52 on May 3, 2008

H0TSauce
Mar 12, 2002

I've got a script that i've got running on a cron schedule. A little while ago, i rewrote the script to also allow it to be run from the web. However, one thing that's screwing up consistently is the file paths. if i call http://www.domain.com/script.php the relative pats in the file are interpreted differently than if it's being called like php -f /var/www/foo/bar/script.php .

Is there a reliable check i can perform in my script that will see whether it's being run via the web versus being called from the command line? This way i'll be able to modify the file paths within the file depending on how the script is being run.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Why not just use absolute paths?

xobofni
Mar 28, 2003
Is there some way to use anonymous arrays? For example, echo (array(1,2,3))[1] would output "2".

H0TSauce
Mar 12, 2002

duz posted:

Why not just use absolute paths?

because i'm obviously a complete moron. I just assumed that the webserver in parsing the path wouldn't understand /var/www/bla/bla and try and tack that on to the virtual host root path.

:bang:

Adbot
ADBOT LOVES YOU

Munkeymon
Aug 14, 2003

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



xobofni posted:

Is there some way to use anonymous arrays? For example, echo (array(1,2,3))[1] would output "2".

php:
<?
echo current(array_slice(array('ZERO','one','two','three','four'),2,1));
?>
Will output 'two'

Someone with more experience probably knows a better way, though.

Edit: if there is no better way, I already have a function laying around that encapsulates that crap:

php:
<?
function anonArrayVal($theArray,$index)
{
    return current(array_slice($theArray,$index,1));
}
?>

Munkeymon fucked around with this message at 17:34 on May 5, 2008

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