By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

Easy PHP question from clueless

P: 27
Hi....I have a question about PHP, which I know nothing about. I was wondering what to do with some php code that I came across on the internet and I wanted to see how it works. Using this page as an example, let's say the code will count every letter on this page and give an answer. How do I go about getting the code to work? Do I simply insert it anywhere in the source code or does it require 'compiled' like some other languages?
Feb 3 '07 #1
Share this Question
Share on Google+
15 Replies


Motoma
Expert 2.5K+
P: 3,235
Hi....I have a question about PHP, which I know nothing about. I was wondering what to do with some php code that I came across on the internet and I wanted to see how it works. Using this page as an example, let's say the code will count every letter on this page and give an answer. How do I go about getting the code to work? Do I simply insert it anywhere in the source code or does it require 'compiled' like some other languages?
Provided your web-server has PHP installed, you can insert it into any file with a .php extension inside <?php ?> brackets.
Feb 3 '07 #2

P: 27
Thank you for the quick reply. So let's say I have this:
<?php
blah
blah
blah
?>

...and I'm on this forum page. I can view the source code, then paste that in to it? Then I have to 'save as' *.php and it should run and count the page characters for me?
Feb 3 '07 #3

Motoma
Expert 2.5K+
P: 3,235
Thank you for the quick reply. So let's say I have this:
<?php
blah
blah
blah
?>

...and I'm on this forum page. I can view the source code, then paste that in to it? Then I have to 'save as' *.php and it should run and count the page characters for me?
Yes, provided all of the php code is correct.
Feb 3 '07 #4

P: 27
Thank you! I'll give it a try.
Feb 3 '07 #5

P: 27
Well, it just makes the code visible on the actual page so I figured I should post specifics. Below is the challenge and the php code:

[PNG image=small black box with white dots throughout]
The pixels in the above image are numbered 0..99 for the first row, 100..199 for the second row etc. White pixels represent ascii codes. The ascii code for a particular white pixel is equal to the offset from the last white pixel. For example, the first white pixel at location 65 would represent ascii code 65 ('A'), the next at location 131 would represent ascii code (131 - 65) = 66 ('B') and so on.

The text contained in the image is the answer encoded in Morse, where "a test" would be encoded as ".- / - . ... -"
You have 15 seconds time to send the solution

[PHP]

<?php
error_reporting(E_ALL ^ E_NOTICE);
$im = ImageCreateFromPng("PNG.png");


for ($y = 0; $y < 30; $y++) {
for ($x = 0; $x < 100; $x++) {
$rgb = ImageColorAt($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ($b == 1) {
$xcoord[] = $x;
$ycoord[] = $y;
}
}
}


for ($i = 0; $i < count($xcoord); $i++) {
$xcoord[$i] += $ycoord[$i] * 100;
$coord[$i] = $xcoord[$i] - $xcoord[$i-1];
$morse .= chr($coord[$i]);
}

echo $morse . "<br />";

include 'morsemod.php';

$result = morse_decode($morse);

echo $result;

?>
[/PHP]
Feb 3 '07 #6

Motoma
Expert 2.5K+
P: 3,235
Does your server have PHP installed? Is the extension on the file .php? Are you viewing the page through your webserver, or by opening it locally?
Feb 3 '07 #7

P: 27
I'm not sure if my webserver has php enabled. The page where the image is viewed is online and the solution to it changes with every failed attempt. As for the php code, I came across it on a site in a forum similar to this one and I copied and pasted it into Notepad for the time so I could have it handy. Now, I have and account at t35.com, which is a free webhosting site, and I put the code in there and saved it as a .php file, if that helps anything.
Feb 3 '07 #8

Motoma
Expert 2.5K+
P: 3,235
I'm not sure if my webserver has php enabled. The page where the image is viewed is online and the solution to it changes with every failed attempt. As for the php code, I came across it on a site in a forum similar to this one and I copied and pasted it into Notepad for the time so I could have it handy. Now, I have and account at t35.com, which is a free webhosting site, and I put the code in there and saved it as a .php file, if that helps anything.
And you said it only printed out the php code? I'm not sure what the problem is. T35.com claims that all users are given PHP access. Do you have a link that I could take a look at?
Feb 3 '07 #9

P: 27
Yeah, at t35.com I saved the file as a .php but I guess I'm not sure what to do with it from there. Do I open the website with the PNG and then open the file from t35? I'm not sure how that would get the script to run.
Feb 5 '07 #10

Motoma
Expert 2.5K+
P: 3,235
Well, as it is set up right now, you would need to upload the png to the server and set it the the filename you are trying to open in your script.
Feb 5 '07 #11

P: 27
Well, as it is set up right now, you would need to upload the png to the server and set it the the filename you are trying to open in your script.
Motoma, here is the site with the challenge. Could you take a quick look at it and see if it's possible to use that .php script to accomplish this? Thank you
http://www.hackthissite.org/missions/permprogramming/2/
Feb 5 '07 #12

Motoma
Expert 2.5K+
P: 3,235
Dude, I'm not going to help you with a hacking challenge for anoter site.
Take a look at PHP.net's GD reference page. Then take a look at the source for the page, side by side with PHP.net's cURL reference.
Learning is the most important part of hacking.
Feb 5 '07 #13

P: 27
Yeah, I figured that if I posted the website, it might get a negative reaction but for me it's just about having harmless fun and learning things in the process. I had to write a simple program to pass one challenge and it was a rewarding feeling once I finished it. I used python for that one but like I said, I came across this php script and was curious as to how it worked and I wanted to see it in action. Thanks again for the replies & for the links.
Feb 6 '07 #14

P: 27
One more question.....in the code it has this: include 'morsemod.php'; Is that like with other languages where it's making a call to another script that has to be accessed for the entire code to work, or is that just a reference to the actual code itself?
Feb 6 '07 #15

Motoma
Expert 2.5K+
P: 3,235
One more question.....in the code it has this: include 'morsemod.php'; Is that like with other languages where it's making a call to another script that has to be accessed for the entire code to work, or is that just a reference to the actual code itself?
That is exactly what that means.
Feb 6 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.