iPXE discussion forum
PHP logic not working - Printable Version

+- iPXE discussion forum (https://forum.ipxe.org)
+-- Forum: iPXE user forums (/forumdisplay.php?fid=1)
+--- Forum: General (/forumdisplay.php?fid=2)
+--- Thread: PHP logic not working (/showthread.php?tid=7360)



PHP logic not working - jhonny - 2014-06-20 09:02

Hi Guys,

I'm trying to get iPXE working with PHP and MYQSL.

For some reason the PHP logic does not seem to be working.

I've got the following code in a php file that ipxe chain boots:
What its supposed to do is grab the serial number of the server and assign it to the variable: serversn
Code:
<?php
header("Content-type: text/plain");
echo "#!ipxe\n\n";

$serversn = "\${serial}";

The problem is when i then do:

Code:
echo $serversn;

The output will look something like this:

Code:
ABCDEFG

So the output is 7 alpha numeric characters long

however when i run:

Code:
echo strlen($serversn);

The output is equal to 9 !!!! instead of 7 !!!!

No idea why that is, so i tried to add some padding on either side of the $serversn variable by doing the following:

Code:
echo str_pad($serversn, 9, "X", STR_PAD_RIGHT);
echo $serversn;

The output should be: ABCDEFGXX
The output is still the original 7 alphanumeric characters: ABCDEF
Its only until that i change the str_pad parameter to 10 that one X will show up:

Code:
echo str_pad($serversn, 10, "X", STR_PAD_RIGHT);
echo $serversn;
OUTPUT is: ABCDEFGX

Can anyone advise what the heck is going on ? why is the count incorrect ?
I've also tried doing the following:

Code:
$serversn = substr($serversn,0,7);

That should cut the string contents of the $serversn variable to the first 7 characters.
Instead what happens is when i echo out $serversn, it returns the following:

Code:
${seria

The output is 7 characters long but the "value" of the variable is lost.

What is going on here ?


RE: PHP logic not working - mastacontrola - 2014-06-20 11:25

(2014-06-20 09:02)jhonny Wrote:  Hi Guys,

I'm trying to get iPXE working with PHP and MYQSL.

For some reason the PHP logic does not seem to be working.

I've got the following code in a php file that ipxe chain boots:
What its supposed to do is grab the serial number of the server and assign it to the variable: serversn
Code:
<?php
header("Content-type: text/plain");
echo "#!ipxe\n\n";

$serversn = "\${serial}";

I think the problem is the calling factor. ${serial} is a place holder for the iPXE calls, so can the reason why the output is 9 characters is because the actual information stored in serversn is:
${serial}, literally. Which, as it happens, is 9 characters.

The way I pass variables like such back to my php scripts is to send it back in the url.

So I might have something like:
PHP Code:
print "#!ipxe\n";
print 
"chain http://192.168.1.2/boot.php?serversn=\${serial}\n"

Then I use the GET or POST var of servsn, this should now be set to the serial, not literally set to ${serial}.

So my Logic would contain, to check it, something like:
PHP Code:
if ($_GET['serversn'])
{
print 
"#!ipxe\n";
print 
"echo My system's serial number is: ....".$_GET['serversn']."\n";

Hope this helps.


RE: PHP logic not working - jhonny - 2014-06-20 12:01

AWESOME ! Thank you so much !

I use the following command to invoke the php script and it works like a charm !:

Code:
chain http://192.168.1.2/boot.php?serversn={serial}

Now when i run:

Code:
echo strlen($_GET['serversn']);

I get the correct length Smile

I wonder if $_GET['serversn'] can be assigned to a variable ?
YEP I CAN ! Sweeeeet !

Code:
$serversn=$_GET['serversn'];
print "echo $serversn";



RE: PHP logic not working - mastacontrola - 2014-06-20 12:45

If you want see a somewhat complicated, but functional, php script that dynamically creates the boot script, you could take a look at this file:
https://sourceforge.net/p/freeghost/code/HEAD/tree/trunk/packages/web/lib/fog/BootMenu.class.php