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
Inquisitus
Aug 4, 2006

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

gibbed posted:

Drop the trailing ?> too, there is no need for it in this script.

code:
<?php
header('Content-Type: image/jpeg');
readfile('http://path/.jpg');

That's just taking it too far :colbert:

gibbed posted:

Edit: wait why the hell did you put a \r\n in the header() call?

That was me getting confused between header and mail (which I'd just been using and takes headers as a string, hence needing line breaks).

Adbot
ADBOT LOVES YOU

Hobo
Dec 12, 2007

Forum bum
This is a more general question rather than a what's wrong with this code question.

I need to adjust a login screen so that when the user enters details and submits, it logs them into both a system that being developed, as well as phpBB3. Note that while the actual database for the two is the same, the system user table is different from the phpBB3 user tables for various reasons.

Basically the way this should work is fill in form, and get logged into the system and phpBB3 at the same time, but I'm having trouble figuring out how phpBB3 handles that sort of thing.

Evil Angry Cat
Nov 20, 2004

Hobo posted:

This is a more general question rather than a what's wrong with this code question.

I need to adjust a login screen so that when the user enters details and submits, it logs them into both a system that being developed, as well as phpBB3. Note that while the actual database for the two is the same, the system user table is different from the phpBB3 user tables for various reasons.

Basically the way this should work is fill in form, and get logged into the system and phpBB3 at the same time, but I'm having trouble figuring out how phpBB3 handles that sort of thing.

Follow the path of the form being submitted and you'll find where phpBB is handling the request, from there just input your own login code. The action attribute of the login form will say something like "login.php?step=2" then just find that part of login.php and hey presto, you can use the login.

Hobo
Dec 12, 2007

Forum bum

Evil Angry Cat posted:

Follow the path of the form being submitted and you'll find where phpBB is handling the request, from there just input your own login code. The action attribute of the login form will say something like "login.php?step=2" then just find that part of login.php and hey presto, you can use the login.

What do I do about the SID that phpBB3 passes? Not sure how it generates that.

Evil Angry Cat
Nov 20, 2004

Hobo posted:

What do I do about the SID that phpBB3 passes? Not sure how it generates that.

If you're just hijacking the login so you can log yourself in elsewhere you don't need to follow that. You want it so you login you user to your main site as well as phpbb when they login? Well just find where phpbb logs in and add your own login code to that section, no need to follow phpBB's lead.

Hobo
Dec 12, 2007

Forum bum

Evil Angry Cat posted:

If you're just hijacking the login so you can log yourself in elsewhere you don't need to follow that. You want it so you login you user to your main site as well as phpbb when they login? Well just find where phpbb logs in and add your own login code to that section, no need to follow phpBB's lead.

The problem is, it needs to be the other way, i.e. adding phpBB's login code to the main site's code, rather than vice versa. If it was the other way around, it would be simpler as I could look at a few plugins I know about, but hey, requirements documents and all that prevent me from doing it the simple way. :/

Are you saying it would be possible to just totally ignore the SID?

LP0 ON FIRE
Jan 25, 2006

beep boop
I would like to sort file names with file extensions numerically. It's not sorting correctly right now, and I think it is because of the file extensions.

So far I have:

code:
<?php

$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false){
	if ($file != "." and $file != ".." and $file != "allBlocks.php"){
		array_push($files, $file);
	}
}
closedir($dir);
rsort($files);
foreach ($files as $file)
echo "<img src=".$file.">";

?>

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


You probably want to do a Natural Sort.

gibbed
Apr 10, 2006

Also suggesting use of glob() instead of the opendir crap.

LP0 ON FIRE
Jan 25, 2006

beep boop

duz posted:

You probably want to do a Natural Sort.

Yes! This is awesome. Exactly what I wanted. Now, I'm trying to make it go in reverse order like before, and I can do it but I don't know if it's the best way because it seems to be working very slow, but then again it could just be my internet connection.

code:
<?php

$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false){
	if ($file != "." and $file != ".." and $file != "allBlocks.php"){
		array_push($files, $file);
	}
}
closedir($dir);
natsort($files);
foreach (array_reverse($files) as $file)
echo "<img src=".$file.">";

?>

LP0 ON FIRE
Jan 25, 2006

beep boop

gibbed posted:

Also suggesting use of glob() instead of the opendir crap.

But do I really need it? Is there a reason this would be better in my situation?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


awdio posted:

php:
<?php
natsort($files);
foreach (array_reverse($files) as $file)
echo "<img src=".$file.">";

?>


It's slow because you're reversing the array each time you loop.

LP0 ON FIRE
Jan 25, 2006

beep boop

duz posted:

It's slow because you're reversing the array each time you loop.

Yeah I see that, but even if I put the array_reverse($files) after natsort, it won't permanently do anything because its not resorting the array. Also putting rsort after the natsort will do away with the natsort.

It doesn't seem too bad now, and maybe it was just my connection, but is there a better way I should do this?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


awdio posted:

Yeah I see that, but even if I put the array_reverse($files) after natsort, it won't permanently do anything because its not resorting the array. Also putting rsort after the natsort will do away with the natsort.

It doesn't seem too bad now, and maybe it was just my connection, but is there a better way I should do this?

Read the docs. array_reverse returns the reversed array. You'll need to do $files = array_reverse($files);.

LP0 ON FIRE
Jan 25, 2006

beep boop

duz posted:

Read the docs. array_reverse returns the reversed array. You'll need to do $files = array_reverse($files);.

Well that was all too obvious. :doh:

Thanks. I probably would have thought to do that after taking a break, but yes it does work a bit faster.

Zedlic
Mar 10, 2005

Ask me about being too much of a sperging idiot to understand what resisting arrest means.
Here's a reasonably silly question:

I'm writing a script that fetches data from a MySQL database and displays it. Instead of directly writing each row to the screen after I've read it from the database (with mysql_fetch_assoc()), I'm thinking of storing the data in a two-dimensional array where data[0]['id'] would be the value of the column named "Id" in the first row. I'm doing this so I can use the data in the table for other things, like exporting it to an excel or text file, without traversing the table every time.

The problem is that the table has 14 columns, and thousands of rows. The average query will return about 2.000 rows. So that's an array with around 30.000 strings, and quite possibly a lot more.

I'm not too experienced with PHP, is an array this big a silly thing to do? How big is too big?

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
Without setting a cookie, session or global variable, I need to pass a variable from index.php to a PHP script included as an image.
eg
php:
<?
// index.php
global $inIndex = true;

[code]

<img src='showImage.php?param1=yay&param2=wee'>
?>
php:
<?
// showImage.php
global $inIndex;

if ($inIndex == true)
 { //show real image }
else
 { //show fuckoff.jpg }
?>

Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!

Zedlic posted:

The problem is that the table has 14 columns, and thousands of rows. The average query will return about 2.000 rows. So that's an array with around 30.000 strings, and quite possibly a lot more.

I'm not too experienced with PHP, is an array this big a silly thing to do? How big is too big?

It depends what problem you are trying to solve and what resources you have in greatest abundance. The situation that I've encountered large arrays in most is when creating reports from large amounts of data; in this case I would say yes, if it's going to save you a few massive queries or expensive joins, do it. But if this is something you want to do every time someone loads a page on your website then you might have a problem.

Try it out by running your script and printing out the value of get_peak_memory_usage() at the end, to see how much memory it used.

Mashi
Aug 15, 2005

Just wanted you to know your dinner's cold and the children all agree you're a shitheel!

Safety Shaun posted:

Without setting a cookie, session or global variable, I need to pass a variable from index.php to a PHP script included as an image.

I'm not quite sure but I think you are trying to prevent hotlinking:

Check the value of $_SERVER['HTTP_REFERER'] and deny access if:
a: There is a url, AND:
b: The address in that URL is not your website (I don't think you can safely rely on an accurate referrer always being present).

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

Mashi posted:

Check the value of $_SERVER['HTTP_REFERER'] and deny access

Ahh yes of course, thank you.

If nobody chimes in with the way I originally planned I'll do that.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Safety Shaun posted:

Without setting a cookie, session or global variable, I need to pass a variable from index.php to a PHP script included as an image.
eg
php:
<?
// index.php
global $inIndex = true;

[code]

<img src='showImage.php?param1=yay&param2=wee'>
?>
php:
<?
// showImage.php
global $inIndex;

if ($inIndex == true)
 { //show real image }
else
 { //show fuckoff.jpg }
?>

This is technically what you asked for :P
php:
<?
[code]
$inIndex = 'true';
echo "<img src='showImage.php?param1=yay&param2=wee&inIndex=$inIndex'>";
?>
php:
<?
// showImage.php
if ( isset($_GET['inIndex']) && $_GET['inIndex'] == 'true') {
 //show real image 
}else{
 //show fuckoff.jpg 
}
?>
But what you are actually asking to do requires sessions or database accesss (write IP and key to DB, pass image script key look it up, see if IP matches) I think.

Lumpy fucked around with this message at 03:33 on Apr 17, 2008

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Safety Shaun posted:

Ahh yes of course, thank you.

If nobody chimes in with the way I originally planned I'll do that.
Isn't this the same question that you asked that 3 people answered on the last page? http://forums.somethingawful.com/showthread.php?threadid=2802621&userid=0&perpage=40&pagenumber=7#post342302162

PowderKeg
Apr 5, 2003
Php noob. I built an html form with some mandatory fields with an i-frame "status" window at the bottom. On submit, the script fires in the i-frame and checks for proper date formats, empty values, etc. and either alerts the user to missing data, or says "Record successfully entered". The frame is helpful because if there are mistakes they can just fix the error and hit submit without hitting back or refilling the whole thing.

Is there a way to have the php script either blank the form on a successful submit, or redirect everythiong to a full "record successfully entered" page? Because as it stands now people can keep clicking submit and load up my db with dupes.

PowderKeg fucked around with this message at 16:51 on Apr 17, 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.
What you are doing seems to make no sense. It seems like you are using an i-frame to mock up browser side validation that javascript was made for.


As for the question, yes, there is, you can do a header redirect to redirect the page after the processing is finished, but if you add JS form validation, and have the form action submit to a processing page that displays the thank you after it's done, you won't need that.

Zorilla
Mar 23, 2005

GOING APE SPIT

PowderKeg posted:

Is there a way to have the php script either blank the form on a successful submit, or redirect everythiong to a full "record successfully entered" page? Because as it stands now people can keep clicking submit and load up my db with dupes.

The form should probably submit to the page it's on, then in the PHP on that page, have it redirect to a success page if all goes well after submitting (like here).

LP0 ON FIRE
Jan 25, 2006

beep boop

gibbed posted:

Also suggesting use of glob() instead of the opendir crap.

Okay, glob is way better you're right. Gets rid of a lot of code and I don't have to tell it to open or close the directory. Definitely using this from now on. Thanks.

LastCaress
May 8, 2004

bonobo
Well I've bought a game engine on an impulse buy. Unfortunately the project was not really active and while they sold me the game very fast, they never responded to my information requests after they had my money (the engine is the VPet v3.0). The engine is full of bugs and incomplete, so I've been editing it to make it work. The problem is I'm a veterinarian with absolutely no coding experience. PHP seems logical enough and I've already managed to implement a player vs player system (yay me) but I'm having trouble with a lot of stuff (and spending hours to discover that php had problems with "" and '' was not much fun). The game can be found at https://www.bazul.org/rpg (it's partially in portuguese so not very interesting for you guys).

The problems I'm having right now:

1-The enemy player table is too big

(http://www.bazul.org/rpg/battle_pvp.php?game=1). I want it to only return 20 results and the others would have a "next page" "previous page" link. This is my code (and don't laugh at me I have no idea what I'm doing!)

php:
<?
$getEnemies = mysql_query("SELECT * FROM user_pets2 WHERE 1 > '0' AND game = '$game' ORDER BY two_p_wins DESC, two_p_loses ASC");
echo"  
<link href='/rpg/pretty.css' type='text/css' rel='stylesheet'>
<table summary=' ' width='95%' border='0'>
<tbody><tr>
    <th scope='col' width='23%' align='left'><strong>Pet Name</strong>
        </td>    </th>
<th scope='col' width='11%' align='left'><strong>Level</strong> </th>
<th scope='col' width='13%' align='left'><strong>Strength</strong>
    </td>
</th>
  <th scope='col' width='11%' align='left'><strong>HP</strong></th>

<th scope='col' width='11%' align='left'><strong>Wins(PvP)</strong>
    </td>
</th>
  <th scope='col' width='13%' align='left'>Losses(PvP)</th>
  <th scope='col' width='9%' align='left'><strong>Owner</strong></th>
  <th scope='col' width='9%' align='left'><strong>Fight</strong></th>
</tr>
";
$x = 0;
while ($array = mysql_fetch_array($getEnemies))
{
    $id = $array[id];
    $y = $x % 3;
    if ($y == 0)
    {
        echo "<tr width=100%>";
    }

    $getOwner = fetch("SELECT * FROM members2 WHERE id = '$array[owner]' AND game = '$game'");
    
    echo "
    <tr><td valign='top'><b>$array[name]</b></td><td valign='middle'>$array[level]</td><td valign='bottom'>
$array[strength]</td><td valign='middle'>$array[max_hp]</td><td valign='middle'>$array[two_p_wins]</td>
<td valign='top'>$array[two_p_loses]</td><td valign='top'>$getOwner[username]</td>
    ";
    if ($userid != $array[owner])
    {
    echo"
    <td valign='top'><a href=battle_pvp.php?game=$game&id=$array[id]&act=prestart>Challenge</a></td></tr>";
    }
    if ($userid == $array[owner])
    {
    echo "
    <td valign='top'>Own Pet
    </td></tr>
    ";
    }
    
    if ($y == 2)
    {
        echo "</tr>";
    }
    $x++;
}

echo "<tbody></table></p>";
?>
2-I'd like to implement a player vs player turn by turn game. The pvp game I "coded" was basically a rewrite of the PvE game that was in place, with some corrections. Basically you just fight another player's pet like it was a regular enemy, but he has no interaction. Since this requires more time I'd like to know if it's possible to request help and offer a forum's upgrade or something. This shouldn't be that hard I guess, because the engine is pretty simple, but I'd guess it requires at least some hours of work.

Thanks!

LastCaress fucked around with this message at 10:55 on Apr 19, 2008

other people
Jun 27, 2004
Associate Christ
I am dumb and I am amazed I have never hit this problem before.

php:
<?
if (! $disable_field) {
    foreach ($form as $field) {
        if (array_key_exists('disabled', $field)) {
            unset($field['disabled']);
        };

    # If I print_r $field here 'disabled' is gone! Yay!

    }:
};

# If I print_r here 'disabled' is not gone wtf!
?>
Apparently I am working with some copy in my foreach loop? What do I do here?

edit: ok, I sort of understand what is going on here. $field is a copy of the data in $form. I am just used to reading through arrays or copying relevant stuff into new ones, I guess this is the first time I have actually tried to edit them.

So could I not do unset($form[$field]['disabled']) in place of my current unset()? It isn't working either!

edit 2: o snap

php:
<?
if (! $disable_field) {
    foreach ($form as $fieldname => $field) {
        if (array_key_exists('disabled', $field)) {
            unset($form[$fieldname]['disabled']);
        };
    }:
};
?>
in my first try $field is an array, not the key name inside $form, so doing unset($form[$field]['disabled']) isn't going to work or make any sense at all. Thank you irc.

other people fucked around with this message at 18:01 on Apr 19, 2008

opblaaskrokodil
Oct 26, 2004

Your morals are my morals. Your wishes are my code.
Well, I have no idea what VPet is like.

LastCaress posted:

I want it to only return 20 results and the others would have a "next page" "previous page" link. This is my code (and don't laugh at me I have no idea what I'm doing!)

php:
<?
$getEnemies = mysql_query("SELECT * FROM user_pets2 WHERE 1 > '0' AND game = '$game' 
ORDER BY two_p_wins DESC, two_p_loses ASC");
?>
Why are you using a condition of 1 > '0' here?
For what it's worth, you could combine your select in the loop with the original query by joining the members2 table on user_pets2.owner = members2.id.

You can achieve a limited range of return results with LIMIT $a, $b at the end of the statement
where $a is the lower limit, and $b is how many you want.
So taking your original query of
code:
SELECT * FROM user_pets2 WHERE 1 > '0' AND game = '$game' 
ORDER BY two_p_wins DESC, two_p_loses ASC
You could add in a LIMIT clause to get some portion of them (just modifying original query)

code:
SELECT * FROM user_pets2 WHERE 1 > '0' AND game = '$game' 
ORDER BY two_p_wins DESC, two_p_loses ASC LIMIT 20, 20
would retrieve the second page of results (assuming 20 per page).



-----------------------------------------------


I just started working with CakePHP (and the whole MVC thing), and I was wondering how to access a model from within a component.

MVC Class Access Within Components talks about how to get access to the controller, but if I'm using the component from some other controller it also doesn't necessarily have access to the model I want. Am I going about this in the wrong way, or is there some way to solve this?
(apart from the hackish / bad
php:
<?
include_once("../../cake/libs/model/model.php");
include_once("../app_model.php");
include_once("../models/modelIWant.php");
?>
sort of thing)

opblaaskrokodil fucked around with this message at 08:04 on Apr 20, 2008

opblaaskrokodil
Oct 26, 2004

Your morals are my morals. Your wishes are my code.
Sorry, double posted it.

LastCaress
May 8, 2004

bonobo
Well the 1<0 condition was probably when I just started and was removing other code so I guess that was left behind... The limit thing worked, thanks!

EDIT : Damnit, now I realized a part of the code doesn't work with PHP5, I'm going to have to convert it to php5 and have no idea how. I just hope it's not that different :\

LastCaress fucked around with this message at 22:24 on Apr 20, 2008

opblaaskrokodil
Oct 26, 2004

Your morals are my morals. Your wishes are my code.

LastCaress posted:

Well the 1<0 condition was probably when I just started and was removing other code so I guess that was left behind... The limit thing worked, thanks!

EDIT : Damnit, now I realized a part of the code doesn't work with PHP5, I'm going to have to convert it to php5 and have no idea how. I just hope it's not that different :\

Which part doesn't work? PHP5 is generally pretty similar to PHP4 (in that most PHP4 stuff should work fine in PHP5).


(also I got my question answered @ the cakePHP google group, so I guess I don't need an answer for that anymore)

LastCaress
May 8, 2004

bonobo
As I said, my coding experience is null. I can run the server in php4 and the code has no problem, and when I put it in php5 there are problems, so obviously the problem is there. The following code:

code:
<?php

/*

Join club (club_join.pro.php)

*/
ob_start();
$rank_check = 1;
include "global.inc.php";
$clubrank_check = 0;
include "club.inc.php";

if ($getclub[private] == 1)
{
	die(header(error("club.php?game=$game&clubid=$clubid","You can't join a private club.")));
}

if ($clubrank > 0)
{
	die(header(error("club.php?game=$game&clubid=$clubid","You are already a member of this club.")));
}

if ($getmemberdata4[id])
{
	die(header(error("club.php?game=$game&clubid=$clubid","You are already a member of a club.")));
}

mysql_query("UPDATE clubs2 SET members = '$getclub[members]+1' WHERE id = '$clubid' AND game = '$game'");
mysql_query("INSERT INTO club_members2 (club,user,position,game) VALUES ('$clubid','$userid','1','$game')");
header(error("club.php?game=$game&clubid=$clubid","Thank you for joining $getclub[name]."));

?>
gives the following error
Parse error: syntax error, unexpected T_PRIVATE, expecting ']' in /home/virtual/site19/fst/var/www/html/rpg/club_join.pro.php on line 14

Now I guess what you need is that club.inc.php and the line is:

code:
$getclub = mysql_query("SELECT * FROM clubs2 WHERE id = '$clubid' AND game = '$game'") or die (mysql_error());

Inquisitus
Aug 4, 2006

I have a large barge with a radio antenna on it.
You need to put quotes around associative array indices:

php:
<?
if ($getclub['private'] == 1)
{
    // ...
}

// ...

if ($getmemberdata4['id'])
{
    // ...
}
?>

MononcQc
May 29, 2007

Not really sure this belongs here or the python thread, but whatever.

I'm learning python on the sides and I'm noticing some stuff.

Basically, python will use a bunch of elifs instead of switches/cases (as far as the tutorial told me, and I hope it's right). This means there's no nead for 'break;', but it acts the same.

However, I noticed that PHP doesn't require the break; to be syntactically right.

So I could do:

php:
<?
$x = 1;

switch($x){
case 1: 
    echo "woo";
case 2: 
    echo "haa";
    break;
}
//this echoes 'woohaa'
?>
PHP's got a bad rep as a language for many different aspects. Is it PHP that's loving up, or python that's missing an aspect of switches with ifs and elifs?

(I also found http://simonwillison.net/2004/May/7/switch/, but it doesn't say much about the possibility to move from statement to statement like I did in PHP)

edit: fixed linking

MononcQc fucked around with this message at 16:35 on Apr 21, 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.
I'm pretty sure PHP is working "correctly", as it were, and python is wrong. You can exclude the break in just about any language and it acts the same as your PHP example, as far as I remember.

MononcQc
May 29, 2007

noonches posted:

I'm pretty sure PHP is working "correctly", as it were, and python is wrong. You can exclude the break in just about any language and it acts the same as your PHP example, as far as I remember.

Alright, good to know, and also surprising it's not everything > PHP on this issue, like it seems to be about code-related things according to posts I read here.

LastCaress
May 8, 2004

bonobo

Inquisitus posted:

You need to put quotes around associative array indices:

Thanks, it worked, except for this line that doesn't return the clube name :\

code:
header(error("club.php?game=$game&clubid=$clubid","Thank you for joining $getclub[name]."));

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


LastCaress posted:

As I said, my coding experience is null. I can run the server in php4 and the code has no problem, and when I put it in php5 there are problems, so obviously the problem is there.

The "problem" is that PHP5 has increased security and since the code is poorly written, it's getting tripped up.

Adbot
ADBOT LOVES YOU

Evil Angry Cat
Nov 20, 2004

LastCaress posted:

Thanks, it worked, except for this line that doesn't return the clube name :\

code:
header(error("club.php?game=$game&clubid=$clubid","Thank you for joining $getclub[name]."));

Because it needs to be $getclub['name'] you've left out the quotes again. You have to use ' ' here rather than " " because of how the variable is called in the string.

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