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
gibbed
Apr 10, 2006

Zorilla posted:

If PHP isn't configured, the web server interprets it as text/plain and just sends off the script without processing it. (beaten)
This is especially nasty when the Apache server begins to break down for some reason (hardware failure, etc) and stops handling PHP (been bitten by this, not just once, but twice). Which is why I put any important code outside of webroot now and include it. Can't take that chance again.

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

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

gibbed posted:

This is especially nasty when the Apache server begins to break down for some reason (hardware failure, etc) and stops handling PHP (been bitten by this, not just once, but twice). Which is why I put any important code outside of webroot now and include it. Can't take that chance again.

I see, I guess if that could be a worry then I ought to go find out how to move the file containing database account information to a safe location. Thanks.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Hammerite posted:

I see, I guess if that could be a worry then I ought to go find out how to move the file containing database account information to a safe location. Thanks.

It's pretty easy.. if your web root is at /var/apache/htdocs/ for example, then store your important stuff in a new folder like /var/apache/hidden/ and then include of the full path to the files:

php:
<?
require_once('/var/apache/hidden/top_secret_db_info.php');
?>

Hammerite
Mar 9, 2007

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

Lumpy posted:

It's pretty easy.. if your web root is at /var/apache/htdocs/ for example, then store your important stuff in a new folder like /var/apache/hidden/ and then include of the full path to the files:

php:
<?
require_once('/var/apache/hidden/top_secret_db_info.php');
?>

Thanks, I hadn't hit on the idea of creating a new directory outside of the web root and just directing php to look there. For some reason I wasn't thinking of the idea of just creating a new directory up there; didn't think it would be allowed, or something.

The web root is titled public_html; it sits inside a directory called ABC, so I now have a new directory called DEF sitting inside ABC which contains commonthings.php. The files for the actual site are in a subdirectory of the main website, i.e. in a subdirectory GHI of public_html (which no longer contains a copy of commonthings.php). So the include statement now reads

code:
include("../../DEF/commonthings.php");
It works and I'm assuming it's completely secure as there isn't any way to point my browser to DEF.

(obviously ABC, DEF, GHI stand in for the real names of these things)

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Hammerite posted:

So the include statement now reads

code:
include("../../DEF/commonthings.php");
It works and I'm assuming it's completely secure as there isn't any way to point my browser to DEF.

(obviously ABC, DEF, GHI stand in for the real names of these things)

The only "problem" with that is when you move your including page to a new level in the directory hierarchy. Then "../../" points somewhere else, and everything breaks. Try to use absolute paths as much as possible. But if it works for now, then you done good, and keeping passwords / usernames, etc. outside of web root is always a good thing, no matter how you go about doing it.

Zorilla
Mar 23, 2005

GOING APE SPIT

Lumpy posted:

The only "problem" with that is when you move your including page to a new level in the directory hierarchy. Then "../../" points somewhere else, and everything breaks. Try to use absolute paths as much as possible. But if it works for now, then you done good, and keeping passwords / usernames, etc. outside of web root is always a good thing, no matter how you go about doing it.

It seems cheesy to hardcode the absolute path to files for one particular server. Is there a way to acquire the web server's DocumentRoot path so things can be easily moved from server to server?

jasonbar
Apr 30, 2005
Apr 29, 2005

Zorilla posted:

It seems cheesy to hardcode the absolute path to files for one particular server. Is there a way to acquire the web server's DocumentRoot path so things can be easily moved from server to server?

This will only work when accessed in such a way that this stuff is set (i.e. through Apache and NOT through the command line.)
php:
<?
$doc_root = getenv("DOCUMENT_ROOT");
?>

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Zorilla posted:

It seems cheesy to hardcode the absolute path to files for one particular server. Is there a way to acquire the web server's DocumentRoot path so things can be easily moved from server to server?

php:
<?
//config.php
define("ROOT_PATH", "/home/fletch/www/website.com/");
define("PATH_TO_IMAGES", ROOT_PATH."public/img/");
?>
When you move to a new server you really should only be updating a few lines in a config file. I find it much more convenient to use absolute paths.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Zorilla posted:

It seems cheesy to hardcode the absolute path to files for one particular server. Is there a way to acquire the web server's DocumentRoot path so things can be easily moved from server to server?

php:
<?
$dr = $_SERVER['DOCUMENT_ROOT'];
?>
beaten, but slightly different =)

Lumpy fucked around with this message at 14:38 on Oct 14, 2008

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.
I have found the best way to get the absolute path is this

php:
<?
define("ROOT_PATH", dirname(__FILE__));
?>
That works if the file it is called from is in the root, or you can get the dirname and explode it into an array using DIRECTORY_SEPARATOR as the delimiter, pop some directories off the end, and implode it back into a string and use that as the root path.

This will work either in a command line program or website.

Mine GO BOOM
Apr 18, 2002
If it isn't broken, fix it till it is.

Begby posted:

That works if the file it is called from is in the root, or you can get the dirname and explode it into an array using DIRECTORY_SEPARATOR as the delimiter, pop some directories off the end, and implode it back into a string and use that as the root path.
Can just use dirname(dirname(dirname(XX))) instead of exploding/imploding if you know how many to pop.

Doom Mathematic
Sep 2, 2008
I just use
code:
<?php
 require_once(dirname(__FILE__)."/../../hidden/includes/commonthings.php");
?>
Assuming the internal layout of files is constant, this is server- and directory-independent. You can just copy the whole structure anywhere and it'll still work.

It's true that moving commonthings.php would break this, but 1) that doesn't happen too often and 2) fixing it usually requires a single fairly swift find/replace on the rest of the codebase.

Sparta
Aug 14, 2003

the other white meat
I have this:
php:
<?php
    /* Header Inclusion */
    include("header.php");
    
    $url $_REQUEST['url'];
    
    /* $go is true if $url is set */
    $go = isset($url);
    $purl parse_url($url);
    $host $purl[host];
    
    /* Check to see if $url is set */
    if ($go == FALSE){
        Stuff
    } else {
        /* If Go is yes, then this is what is shown */
        echo $url;
        echo $host;
    }
    
    include("footer.php");
?>

I'm getting url to show up but not $host. I really just need $host. Any ideas as to why this isn't working?

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Sparta posted:

I have this:

I'm getting url to show up but not $host. I really just need $host. Any ideas as to why this isn't working?

Why don't you use $_SERVER['HTTP_HOST']; to get the host? No parsing required.

[EDIT] Try printing $purl with print_r($purl) and see if it's actually setting a host to see if it's the parsing or something else....

Lumpy fucked around with this message at 01:47 on Oct 15, 2008

Sparta
Aug 14, 2003

the other white meat

Lumpy posted:

Why don't you use $_SERVER['HTTP_HOST']; to get the host? No parsing required.

[EDIT] Try printing $purl with print_r($purl) and see if it's actually setting a host to see if it's the parsing or something else....

$url is variable, it's not the current site's host.

Trying print_r now.

Sparta
Aug 14, 2003

the other white meat
Returns Array ( [path] => https://www.mikemanfrin.com/hello )

http://metrics.cc/url.php?url=www.mikemanfrin.com/hello

Sparta fucked around with this message at 05:21 on Oct 15, 2008

sonic bed head
Dec 18, 2003

this is naturual, baby!
I have an image that was created from imagecreatefromjpeg() in a php script. I have PHPMailer up and running but I can't figure out how to send the image as an attachment without first writing the file to disk. Is it possible to send it without it being a file? I can't seem to find any real good documentation for phpmailer and I also don't really know how encoding attachments works so any help would be greatly appreciated. Thanks.

Zorilla
Mar 23, 2005

GOING APE SPIT

sonic bed head posted:

I have an image that was created from imagecreatefromjpeg() in a php script. I have PHPMailer up and running but I can't figure out how to send the image as an attachment without first writing the file to disk. Is it possible to send it without it being a file? I can't seem to find any real good documentation for phpmailer and I also don't really know how encoding attachments works so any help would be greatly appreciated. Thanks.

I believe imagecreatefromjpeg() loads a JPEG into memory, uncompressed and likely taking up seveal MB because of it. That's not something you want to attach to an email. Unless PHPMailer has a method for accepting GD image objects as file attachments where it compresses it back down for you, you'll probably have to save the image to a temporary location using imagejpeg().

Unless PHP provides a graceful way to store files temporarily in 1-2 lines of code, I would just save the file with a random name like md5(session_id()).".jpg", destroy the copy in memory, email it, then delete it.

Zorilla fucked around with this message at 06:54 on Oct 15, 2008

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

:dukedog:

I have a div with a set width and height and a background image.

How do I place images (in other divs possibly) randomly inside the main div? I want to try doing this without them overlapping.

If PHP is overkill, could I do this with Javascript?

Zorilla
Mar 23, 2005

GOING APE SPIT

drcru posted:

I have a div with a set width and height and a background image.

How do I place images (in other divs possibly) randomly inside the main div? I want to try doing this without them overlapping.

If PHP is overkill, could I do this with Javascript?

If the desired result is a slightly jumbled appearance, you might try setting float: left on each image and giving each a random amount of margin within 10-50px.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
edit: nevermind

fletcher fucked around with this message at 19:01 on Oct 15, 2008

KuruMonkey
Jul 23, 2004
Or if you want to have more control, set the containing div to position:relative and the images to position:absolute and set the left/top of the images to precise values. (with style attributes if you're doing this in the PHP, and you could find the image dimensions with GD so you could generate this from any directory of images)

Then you can make sure they fit if you know the width/height of the div and the widths/heights of the images

Depends how random you want it, and how particular about them not overlapping.

With that method you could even overlap them nicely and in a controlled manner using z-order and shuffle them around with javascript, hmmmm...

KuruMonkey fucked around with this message at 19:02 on Oct 15, 2008

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

I do not know much (read: anything) about parse_url. Could it be because it does not have 'http://' in front of it? If *none* of your url vars are going to have [url]http://[/url] in front of them, why not use explode() and then grab the first element of the resulting array?

A Flaming Chicken
Feb 4, 2007

sonic bed head posted:

I have an image that was created from imagecreatefromjpeg() in a php script. I have PHPMailer up and running but I can't figure out how to send the image as an attachment without first writing the file to disk. Is it possible to send it without it being a file? I can't seem to find any real good documentation for phpmailer and I also don't really know how encoding attachments works so any help would be greatly appreciated. Thanks.

PHPMailer is defunct. SwiftMailer is the way to go.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Zorilla posted:

Unless PHP provides a graceful way to store files temporarily in 1-2 lines of code, I would just save the file with a random name like md5(session_id()).".jpg", destroy the copy in memory, email it, then delete it.

http://us.php.net/manual/en/function.tempnam.php

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


:argh:

sonic bed head
Dec 18, 2003

this is naturual, baby!

That works great! I don't have to worry about chmoding some directories, it automatically gets a directory that is writable.

Is there a way to allow execution of a script to continue after someone closes a browser window? I want someone to be able to go to blah.com/sendMeAnEmail.php?email=blah and then just close the browser and have all of the script continue execution that might take some time because it involves an HTTP Request.

edit:I found it. ignore_user_abort(TRUE); Let me just say, I am beginning to get very very annoyed by the odd naming conventions in PHP.

sonic bed head fucked around with this message at 22:11 on Oct 15, 2008

a cat
Aug 8, 2003

meow.
Hey guys. I'm trying to teach myself php basically by writing psudocode and then looking things up on w3schools.

I'm trying to make a php program to shuffle and draw 6 cards (A-6 of spades) from a deck at random. Just to kinda give me an idea of what I was loving up I also had it print the number of cards remaining and their values. I can't get this to work though. Any help would be greatly appreciated. A live version of the site can be found at http://www.furbyarmy.com/poker.php

code:
<?php
function Drawfromdeck($cardsdrawn)
{	

$deck = array("As","2s","3s","4s","5s","6s");
$i=0;
while($i<$cardsdrawn)
	{
	$j = $i + 1;
	$random = mt_rand(0,count($deck));
	$card = $deck[$random];
	echo "Draw " . $j . ": " . $card . "<br />";
	
	$i++;
	
	echo "deck count: " . count($deck) . "<br />remaining cards in deck: <br />";
	$n = 0;
	while ($n < count($deck))
	{
	echo $deck[$n] . ", ";
	$n++;
	if ($n == $cardsdrawn)
		{
		echo "<br /><br />";
		}
	}

	}

}


Drawfromdeck(5);

?>

a cat fucked around with this message at 00:17 on Oct 17, 2008

J. Elliot Razorledgeball
Jan 28, 2005

jstirrell posted:

:words:

php:
<?php

function drawFromDeck($cards) {
    
    $deck = array("As""2s""3s""4s""5s""6s");
    
    for($i 0$i $cards$i++) {
        shuffle($deck);
        $card array_pop($deck);
        
        print "Drew $card<br>\n";
        print "Remaining cards:<br>\n";
        foreach($deck as $k => $v) {
            print $v " ";
        }
        print "<br>\n";
        print "---------"<br>\n";
    }
}

print drawFromDeck(5);
?>

http://rulethepla.net/drawcards.php

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

J. Elliot Razorledgeball posted:

php:
<?

        print "---------"<br>\n";
?>
http://rulethepla.net/drawcards.php

Syntax error on that line.

simcole
Sep 13, 2003
PATHETIC STALKER
I had a friend write me some code. I don't understand what -> does. Can someone link me an article and explain it?

Also is $this just a variable or a defined variable that means something special in php?

code:
function connect() {
        if ($this->obj['persistent']) {
            $this->connection_id = mysql_pconnect( $this->obj['sql_host'], $this->obj['sql_user'],$this->obj['sql_pass']);
        } else {
            $this->connection_id = mysql_connect( $this->obj['sql_host'], $this->obj['sql_user'], $this->obj['sql_pass']);
        }
        if ( !mysql_select_db($this->obj['sql_database'], $this->connection_id) ) {
            echo ("ERROR: Cannot find database");
        }
    }

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

simcole posted:

I had a friend write me some code. I don't understand what -> does. Can someone link me an article and explain it?

Also is $this just a variable or a defined variable that means something special in php?



-> is the equivalent of the "." in most languages. An English translation would be " method or property of " an object.

$blah->talk(); is like "call the talk() method of the $blah object" and
echo $pie->flavor; is "echo the flavor property of the $pie object"

$this is a self-reference. If I have code inside an object, $this refers to the object said code is running in.

simcole
Sep 13, 2003
PATHETIC STALKER
Thanks so much. I knew what . was but I couldn't find -> anywhere. I figured $this was a keyword but I never saw it defined. Thanks agian.

Little Brittle
Nov 1, 2004

Come visit me dawg
This is more of a caching question, but is there a good way to serve a cached PHP page while adding to total page views? Additionally, what is the most efficient way to track page views? I'm thinking that an "UPDATE page_views" query on every page load is probably not the best way.

J. Elliot Razorledgeball
Jan 28, 2005

Lumpy posted:

Syntax error on that line.

whoops

php:
<?
print "--------<br>\n";
?>

Zorilla
Mar 23, 2005

GOING APE SPIT

Lumpy posted:

-> is the equivalent of the "." in most languages. An English translation would be " method or property of " an object.

One thing I've never found documentation on is what the &= assignment operator is supposed to be. Or why I sometimes see functions as ___somename(). Can somebody tell me what the purpose of these are?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Zorilla posted:

One thing I've never found documentation on is what the &= assignment operator is supposed to be. Or why I sometimes see functions as ___somename(). Can somebody tell me what the purpose of these are?

=&
__somename()

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


double post :argh:

Atom
Apr 6, 2005

by Y Kant Ozma Post

Zorilla posted:

One thing I've never found documentation on is what the &= assignment operator is supposed to be. Or why I sometimes see functions as ___somename(). Can somebody tell me what the purpose of these are?

&= is "bitwise AND" assignment.

$a = $a & 16;
$a &= 16;

are equivalent statements.

Functions like __function() are so-called "magic functions" and are reserved for future use by PHP. No user-defined function, constant, or method should start with "__".

e:f;b.

edit: Ha I was right though. "&=" != "=&"

Adbot
ADBOT LOVES YOU

Zorilla
Mar 23, 2005

GOING APE SPIT

Atom posted:

&= is "bitwise AND" assignment.

$a = $a & 16;
$a &= 16;

are equivalent statements.

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.

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