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
MrEnigma
Aug 30, 2004

Moo!

Zorilla posted:

I should have asked about =& specifically since I saw it here. I still can't quite figure out what it's supposed to do differently than = does.

It makes a reference (which you can read about a few posts up..well the link in a few posts up).

Objects by default are referenced just using =, but when a =& is done, they perform a clone (a shallow copy essentially...sometimes called a hard link).

Think of it this way.

$a = 1;
$b =& $a;

$b++;

// $a will be 2 here.
var_dump($a);

if you did $b = $a, $a would still be 1. Instead of holding a value, it basically just links to another variable...a reference. It gets a bit more complicated when you start using references inside objects and passing in to functions, but it's pretty easy.

Adbot
ADBOT LOVES YOU

Zorilla
Mar 23, 2005

GOING APE SPIT
Oops, why didn't I see that link

Atom
Apr 6, 2005

by Y Kant Ozma Post
$a = 3;
$b = $a; //$b is a copy of $a;
$b++; //changing $b does not change $a
echo "$a != $b"; //Prints 3 != 4

$a = 3;
$b =& $a; //$b is a reference of $a;
$b++; //changing $b DOES change $a
echo "$a == $b"; //Prints 4 == 4

edit: gently caress BEATEN AGAIN YOU ASSHOLES. :mad:

Atom fucked around with this message at 01:03 on Oct 18, 2008

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
What type of situation would it be useful to use the =&? I understand what it does, I just don't understand why you would want that.

sonic bed head
Dec 18, 2003

this is naturual, baby!

fletcher posted:

What type of situation would it be useful to use the =&? I understand what it does, I just don't understand why you would want that.

If you wanted a function that could return two values, you couldn't in php, because each function can only return one value to its caller. The way to get around this would be to send in a pointer to the function as a parameter and allow changes to be done on that. I use pointer here as something that is assigned using the =&.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

sonic bed head posted:

If you wanted a function that could return two values, you couldn't in php, because each function can only return one value to its caller. The way to get around this would be to send in a pointer to the function as a parameter and allow changes to be done on that. I use pointer here as something that is assigned using the =&.

Ohhhhh that makes sense. Whenever I needed to do that I just returned an array with everything I need in it.

MrEnigma
Aug 30, 2004

Moo!

sonic bed head posted:

If you wanted a function that could return two values, you couldn't in php, because each function can only return one value to its caller. The way to get around this would be to send in a pointer to the function as a parameter and allow changes to be done on that. I use pointer here as something that is assigned using the =&.

It also helps when you don't want your data being copied all over the place. Say you're operating on a huge array, and instead of passing it in to a function, you could reference it in. It also comes in very handy when you're using recursive functions.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof
Edit: Oops, question already answered.

DaTroof fucked around with this message at 05:47 on Oct 18, 2008

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

:dukedog:

How can I make this simpler and or better?

php:
<?
if($curr_sector < $start_of_line)
    $curr_sector = -1;
elseif($curr_sector >= $start_of_line + $size)
    $curr_sector = -1;
elseif($curr_sector > $end_sector)
    $curr_sector = -1;?>

KuruMonkey
Jul 23, 2004

drcru posted:

How can I make this simpler and or better?

php:
<?
if($curr_sector < $start_of_line)
    $curr_sector = -1;
elseif($curr_sector >= $start_of_line + $size)
    $curr_sector = -1;
elseif($curr_sector > $end_sector)
    $curr_sector = -1;?>

php:
<?

if(($curr_sector < $start_of_line) || ($curr_sector >= ($start_of_line + $size)) || ($curr_sector > $end_sector))
{
  $curr_sector = -1;
}
?>
And put thise 3 sub-clauses in the order 'most likely to be true in normal usage' first.

(did you really intend the result of all three options to be the same?)

Also consider if its simpler / clearer to test for the cases where you shouldn't be setting curr_sector to -1, and test for that negated:

php:
<?
if(!(SIMPLER_CLAUSE))
{
  $curr_sector = -1;
}
?>
Lastly, if you're using -1 as an 'not valid' value in a variable that normally stores numbers, get into the habit of using FALSE for that - then test for it explicitly:
php:
<?
$val = myfunc();
if($val !== FALSE && YOUR_OTHER_TESTS_ARE_PASSED)
{
  // use $val knowing its valid
}
?>
So; working on the assumption that this is a test for the current sector being valid to use and then setting it to a value that (maybe) causes a loop to terminate... (also took a stab at what might be most likely to be the "normal" fail condition)

php:
<?

if(($curr_sector > $end_sector) || ($curr_sector < $start_of_line) || ($curr_sector >= ($start_of_line + $size)))
{
  $curr_sector = FALSE;
}
?>
which might go nicely with a while($curr_sector !== FALSE) {} loop

KuruMonkey fucked around with this message at 14:57 on Oct 18, 2008

invid
Dec 19, 2002
Just curious,

Any Kohana Framework users here?

cka
May 3, 2004
Yup, I got a site running on Kohana 2.1 (that I have to probably upgrade to 2.2 soon) and a cms for said site in the works. It's a pretty sweet lil' framework.

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

:dukedog:

KuruMonkey posted:

php:
<?

if(($curr_sector < $start_of_line) || ($curr_sector >= ($start_of_line + $size)) || ($curr_sector > $end_sector))
{
  $curr_sector = -1;
}
?>
And put thise 3 sub-clauses in the order 'most likely to be true in normal usage' first.

(did you really intend the result of all three options to be the same?)

Also consider if its simpler / clearer to test for the cases where you shouldn't be setting curr_sector to -1, and test for that negated:

php:
<?
if(!(SIMPLER_CLAUSE))
{
  $curr_sector = -1;
}
?>
Lastly, if you're using -1 as an 'not valid' value in a variable that normally stores numbers, get into the habit of using FALSE for that - then test for it explicitly:
php:
<?
$val = myfunc();
if($val !== FALSE && YOUR_OTHER_TESTS_ARE_PASSED)
{
  // use $val knowing its valid
}
?>
So; working on the assumption that this is a test for the current sector being valid to use and then setting it to a value that (maybe) causes a loop to terminate... (also took a stab at what might be most likely to be the "normal" fail condition)

php:
<?

if(($curr_sector > $end_sector) || ($curr_sector < $start_of_line) || ($curr_sector >= ($start_of_line + $size)))
{
  $curr_sector = FALSE;
}
?>
which might go nicely with a while($curr_sector !== FALSE) {} loop

Oh god, thanks.

None of this was registering last night.

Grand Poobah
Jun 20, 2005
OK, stumped myself again

using mysql 4.1

I've sent data to the page via a form POST, and converted the POST results into variables

I'm trying to run an UPDATE query with the data from the form using the following

$sql = "UPDATE `projects` SET 'ProjName' = $name WHERE `Key` = $key123";
$result = mysql_query($sql)
or die("Couldn't execute result query."); <---this is where I die

I've verified that my variables are correct by running an echo on just them. Any ideas?

vv Thank you. I never would have figured out the difference between ` and '.

Grand Poobah fucked around with this message at 18:23 on Oct 25, 2008

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS
Your SQL looks a little off. Try this:

php:
<?
$sql = "UPDATE `projects` SET `ProjName` = '$name' WHERE `Key` = '$key123'";
$result = mysql_query($sql)
or die("Couldn't execute result query.");
?>

SuckerPunched
Dec 20, 2006

Additionally, you could die with a more useful error:

code:
<? 
$result = mysql_query($sql) or die( mysql_error() );
?>

H0TSauce
Mar 12, 2002

jschmidt posted:

OK, stumped myself again

using mysql 4.1

I've sent data to the page via a form POST, and converted the POST results into variables

I'm trying to run an UPDATE query with the data from the form using the following

$sql = "UPDATE `projects` SET 'ProjName' = $name WHERE `Key` = $key123";
$result = mysql_query($sql)
or die("Couldn't execute result query."); <---this is where I die

I've verified that my variables are correct by running an echo on just them. Any ideas?

Shouldn't you be escaping your php variables from the sql string?

$sql = "UPDATE `projects` SET 'ProjName' = {$name} WHERE `Key` = {$key123}";

or alternatively

$sql = "UPDATE `projects` SET 'ProjName' = ".$name." WHERE `Key` = ".$key123;

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

H0TSauce posted:

Shouldn't you be escaping your php variables from the sql string?

$sql = "UPDATE `projects` SET 'ProjName' = {$name} WHERE `Key` = {$key123}";

or alternatively

$sql = "UPDATE `projects` SET 'ProjName' = ".$name." WHERE `Key` = ".$key123;

If it's inside double quotes, "simple" variables will be replaced. Complex ones (array values, object properties) would need to be escaped, but not here.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

H0TSauce posted:

Shouldn't you be escaping your php variables from the sql string?

$sql = "UPDATE `projects` SET 'ProjName' = {$name} WHERE `Key` = {$key123}";

or alternatively

$sql = "UPDATE `projects` SET 'ProjName' = ".$name." WHERE `Key` = ".$key123;

Use PDO so you can just write nice clean SQL statements, and you never have to worry about escaping a variable again. (this needs to be in the OP, this comes up time and time again)

php:
<?
$query = $database->prepare("SELECT field FROM table WHERE other_field = :value");
$query->bindParam(":value", $whatever);
?>

SuckerPunched
Dec 20, 2006

mysqli also supports prepared statements, but yes prepared statements are the way to go.

Khorne
May 1, 2002
Are there any extremely light-weight CMS systems that are easy to modify to fit in to an existing framework?

Things I am looking for:

Very simple user table (accountId, Username, Password, Email, Access) so it can just use the existing one
News posts/static pages (updates / information pages)
Article types with varying access requirements and an easy way for admins to revert edits (wiki-like system for documentation)
Loads a php file and passes it an array or object with page information (no smarty, no intricate templating systems)

All requirements don't have to be met. I know I am going to have to modify whatever I do choose to use. I cringe at the sight of most CMS systems I've checked out. TikiWiki and MediaWiki are bloated and geared more toward lame wiki sites than practical applications. Most of the smaller CMS projects seem poorly implemented or downright bad.

I'm fairly close to coding it myself, but it's a bad idea for me to take on another project. I have far too many left unfinished and an ever decreasing amount of free time.

Munkeymon
Aug 14, 2003

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



Khorne posted:

TikiWiki and MediaWiki

Have you looked at DokuWiki? I know it's not really a CMS, but neither are those, and it's pretty easy to set up and use.

Edit: http://www.dokuwiki.org/

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

fletcher posted:

Use PDO so you can just write nice clean SQL statements, and you never have to worry about escaping a variable again. (this needs to be in the OP, this comes up time and time again)

php:
<?
$query = $database->prepare("SELECT field FROM table WHERE other_field = :value");
$query->bindParam(":value", $whatever);
?>

I assume PDO cleans bound parameters of nasties, or must one still do that manually?

Begby
Apr 7, 2005

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

Lumpy posted:

I assume PDO cleans bound parameters of nasties, or must one still do that manually?

It cleans them for you.

Khorne
May 1, 2002

Munkeymon posted:

Have you looked at DokuWiki? I know it's not really a CMS, but neither are those, and it's pretty easy to set up and use.

Edit: http://www.dokuwiki.org/
That is perfect. Thanks.

Strong Sauce
Jul 2, 2003

You know I am not really your father.





cka posted:

Yup, I got a site running on Kohana 2.1 (that I have to probably upgrade to 2.2 soon) and a cms for said site in the works. It's a pretty sweet lil' framework.

Have you used CodeIgniter before? Do you feel that there are that many advantages to Kohana's strict PHP5 framework vs CodeIgniter's which supports both PHP4 and 5? How often is Kohana updated? I always wanted to transfer over to Kohana but was always worried about the project being abandoned.

Roctor
Aug 23, 2005

The doctor of rock.
So I'm working with a guy to make a site for the use of people in the musician's lounge subforum. The guy I'm working with got a SA authentication script from the gbsfm guys. At first glance it looks pretty solid, but I can't get it to correctly set the cookies necessary so that the script can check a user's profile for the auth key.

So here's the relevant portion of the script (I hope this isn't crossing any lines with the gbsfm crew):
php:
<?
# The user we want to check, and they key to check
$user = stripslashes('roctor');
$key = '36188438ac1d50cfb5eb7ae34882b988';

# SA parameters. Use your SA user ID and the md5 hash of your password
# This is the user ID of my gimmick account,
$sauserid = '83280';
$sapasswordhash = '(my password hash)';

# Setup values to use in the CURL operation
$url = "http://forums.somethingawful.com/member.php?s=&action=getinfo&username=".urlencode(stripslashes($user));
$cookie = "bbuserid=$sauserid;bbpassword=$sapasswordhash";

# These aren't necessary, but it helps to prevent setting off any alarms
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0';
$referer = 'http://forums.somethingawful.com/usercp.php?s=';

# Initialize CURL and start collecting the output buffer
$ch = curl_init();
ob_start();

# Setup CURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, $referer);

# Execute and close CURL
curl_exec($ch);

# Get the returned HTTP text and release the output buffer
$response = ob_get_contents();
ob_end_clean();
?>
So the curl works correctly and the page is stored in the $response variable, however, the page that is stored is a "you must be logged in" page, rather than the user profile. I kind of suspect the password has I'm using, but I got the value for the password hash directly from the cookie that SA sets with the 'view cookie' firefox addon.

I'm kind of at a loss about how to get the verification working correctly since there's not really a whole lot of easy to find documentation out there. Anybody know what I'm doing wrong here? Could it be some sort of server configuration?

If it's any help, the script is currently up at https://www.musicianslounge.org/test

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Roctor posted:

So I'm working with a guy to make a site for the use of people in the musician's lounge subforum. The guy I'm working with got a SA authentication script from the gbsfm guys. At first glance it looks pretty solid, but I can't get it to correctly set the cookies necessary so that the script can check a user's profile for the auth key.

Contact r1ch and use his AuthDB system instead of trying to scrape the forums yourself.

Roctor
Aug 23, 2005

The doctor of rock.

duz posted:

Contact r1ch and use his AuthDB system instead of trying to scrape the forums yourself.

Nothing makes me happier than this. Thanks for the link/referral.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

duz posted:

Contact r1ch and use his AuthDB system instead of trying to scrape the forums yourself.

I'm confused, this requires a user to enter in their SA password? Or is the 'pass' that you post just whatever code you are looking for in the profile?

Roctor
Aug 23, 2005

The doctor of rock.

fletcher posted:

I'm confused, this requires a user to enter in their SA password? Or is the 'pass' that you post just whatever code you are looking for in the profile?

I contacted r1ch earlier and he told me it's the user's alluvian.org password.

ante
Apr 9, 2005

SUNSHINE AND RAINBOWS

fletcher posted:

I'm confused, this requires a user to enter in their SA password? Or is the 'pass' that you post just whatever code you are looking for in the profile?

Here's an example of another site that uses this. You must have an Alluvion account, which only requires that you put something in your SA profile to verify. No entering of your SA password anywhere.

So yeah, two separate accounts.

cka
May 3, 2004

Strong Sauce posted:

Have you used CodeIgniter before? Do you feel that there are that many advantages to Kohana's strict PHP5 framework vs CodeIgniter's which supports both PHP4 and 5? How often is Kohana updated? I always wanted to transfer over to Kohana but was always worried about the project being abandoned.

Nah, I haven't really used CI. I read up on a few frameworks and decided on Kohana cause it was strictly written for PHP5, which is fine since that's what we're running on our server (and it seemed like the easiest to get my project off the ground with.) For the most part, Kohana is really easy to work with, and while the documentation isn't as stellar as I'd like it to be it does the job (as does their forum.) What I like about it so far, and I'm not sure if CI has this functionality too, is that I can override core functionality easily with my own libraries. I have my own default controller class that handles session stuff and authorization with users in a vBulletin database in the CMS I talked about.

The big problem with it right now is that it's still in a wild state of development -- they changed a bunch of the core functionality between 2.1 and 2.2, and nothing really feels concrete in it still since there seems to be even more core changes in the 2.3 svn version. My site uses some on-the-fly routing with the _default() class function, which is one of those major changes they made in 2.2. That's partially why I've stuck with 2.1 so far (the other reasons being I've been busy with my other, real job and extreme laziness/procrastination in my spare time.) Plus, if it ain't broke...

I don't think you have to worry about it being abandoned, but if you did want to switch over I'd follow their update trac/changelogs for awhile to make sure they aren't drastically changing how core functionality works.

Strong Sauce
Jul 2, 2003

You know I am not really your father.





cka posted:

Nah, I haven't really used CI. I read up on a few frameworks and decided on Kohana cause it was strictly written for PHP5, which is fine since that's what we're running on our server (and it seemed like the easiest to get my project off the ground with.) For the most part, Kohana is really easy to work with, and while the documentation isn't as stellar as I'd like it to be it does the job (as does their forum.) What I like about it so far, and I'm not sure if CI has this functionality too, is that I can override core functionality easily with my own libraries. I have my own default controller class that handles session stuff and authorization with users in a vBulletin database in the CMS I talked about.

The big problem with it right now is that it's still in a wild state of development -- they changed a bunch of the core functionality between 2.1 and 2.2, and nothing really feels concrete in it still since there seems to be even more core changes in the 2.3 svn version. My site uses some on-the-fly routing with the _default() class function, which is one of those major changes they made in 2.2. That's partially why I've stuck with 2.1 so far (the other reasons being I've been busy with my other, real job and extreme laziness/procrastination in my spare time.) Plus, if it ain't broke...

I don't think you have to worry about it being abandoned, but if you did want to switch over I'd follow their update trac/changelogs for awhile to make sure they aren't drastically changing how core functionality works.
What I actually meant was its stability. I may just wait a while before using it as my primary framework for PHP. CI does have the ability to overwrite almost all functionality such as controllers/helpers/libraries although it seems like Kohana have a lot more versatility using an events system.

LP0 ON FIRE
Jan 25, 2006

beep boop
I'm using imagepng to create PNGs out of code into a certain directory. That all works fine, but why on the page where i use imagepng it spits out weird characters and how do I get rid of them?

To see what I mean, just draw some nonsensical thing on the page, type in the CAPTCHA and then the page where it says it was successful shows that mess.

Just to let you know, depending what people have made, might be considered :nws: not work safe. But they are just pixelated small images.

http://www.majoroutput.com/TestMOCode2/index.php

edit:

I don't know if there's any alternate way of using it, but here's the line of code: imagepng($thumb, "blocks/".$numOfFiles.".png");

If I get rid of that, it takes away the characters.

LP0 ON FIRE fucked around with this message at 02:25 on Oct 26, 2008

Internet Headache
May 14, 2007

awdio posted:

I'm using imagepng to create PNGs out of code into a certain directory. That all works fine, but why on the page where i use imagepng it spits out weird characters and how do I get rid of them?

To see what I mean, just draw some nonsensical thing on the page, type in the CAPTCHA and then the page where it says it was successful shows that mess.

Just to let you know, depending what people have made, might be considered :nws: not work safe. But they are just pixelated small images.

http://www.majoroutput.com/TestMOCode2/index.php

edit:

I don't know if there's any alternate way of using it, but here's the line of code: imagepng($thumb, "blocks/".$numOfFiles.".png");

If I get rid of that, it takes away the characters.
I'm not sure why imagepng would function that way. As a short-term hack for a library of hacks, you could use output buffering around the function and just dump the contents.

php:
<?
ob_start();
imagepng($thumb, "blocks/".$numOfFiles.".png");
ob_end_clean();
?>

LP0 ON FIRE
Jan 25, 2006

beep boop

Internet Headache posted:

I'm not sure why imagepng would function that way. As a short-term hack for a library of hacks, you could use output buffering around the function and just dump the contents.

php:
<?
ob_start();
imagepng($thumb, "blocks/".$numOfFiles.".png");
ob_end_clean();
?>

Excellent! It works, thank you.

dagard
Mar 31, 2005

Internet Headache posted:

php:
<?
imagepng($thumb, "blocks/".$numOfFiles.".png");
?>

Apropos of nothing, please, don't do that. Fan out your directories, don't put everydamnthing in one directory. readdir() calls will start beating the hell out of your performance. A better way would be, let's say $numOfFiles is 38234234, write it to:

blocks/038/234/234.png

Storage, especially if/when you get to the point where you're using shark/netapp/isilon/whatever, is going to plotz if it has to stat() across a directory of 10000+ files. Try to keep it around 1-2k, you're golden, and really, it can scale forever.

(Ex: 3823423482340238423.png translates to 003/823/423/482/340/238/423.png), and you're looking at, max, around 2k entries per directory, which about any OS or storage system should be able to handle

File size versus FS sector size being left as a discussion for later, as is the extended cost of walking up that whole directory tree.

LP0 ON FIRE
Jan 25, 2006

beep boop

dagard posted:

Apropos of nothing, please, don't do that. Fan out your directories, don't put everydamnthing in one directory. readdir() calls will start beating the hell out of your performance. A better way would be, let's say $numOfFiles is 38234234, write it to:

blocks/038/234/234.png

Storage, especially if/when you get to the point where you're using shark/netapp/isilon/whatever, is going to plotz if it has to stat() across a directory of 10000+ files. Try to keep it around 1-2k, you're golden, and really, it can scale forever.

(Ex: 3823423482340238423.png translates to 003/823/423/482/340/238/423.png), and you're looking at, max, around 2k entries per directory, which about any OS or storage system should be able to handle

File size versus FS sector size being left as a discussion for later, as is the extended cost of walking up that whole directory tree.

Thanks. Yes, it will eventually go to different directories. I never thought of the way you suggested, so thank you.

edit:

Isn't that scaling way further into the trillions of possible files?

LP0 ON FIRE fucked around with this message at 00:07 on Oct 27, 2008

Adbot
ADBOT LOVES YOU

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

:dukedog:

With an array like this:

php:
<?
Array
(
    [0] => Array
        (
            [0] => -3
            [1] => -2
            [2] => -1
            [3] => 0
            [4] => -1
        )

    [1] => Array
        (
            [0] => -3
            [1] => -2
            [2] => -1
            [3] => 0
            [4] => -1
        )

    [2] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 1
            [3] => 2
            [4] => 3
        )

    [3] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 26
            [3] => 27
            [4] => 28
        )

    [4] => Array
        (
            [0] => -1
            [1] => -1
            [2] => 51
            [3] => 52
            [4] => 53
        )

)?>
Would it be very memory/cpu intesive if I looped through this and checked for conditions?

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