Jump to content

What Does This Php Code Do?


Norman

Recommended Posts

Try this code:

<?php$pt2="http://$SERVER_NAME$SCRIPT_NAME";echo $pt2 . '<br />';$pt2= substr($pt2,0,strlen($pt2)-21);echo $pt2 . '<br />';?>

The substr() finds a sub-string in a string from a starting point for a given length. In this case, starting at the first (0) character for a length of (string_length minus 21) characters.http://ca3.php.net/substr

Link to comment
Share on other sites

Ok, I understand the second line. Now I still need to understand the first line. In my localhost I get this php error message:

Notice: Undefined variable: SERVER_NAME in C:\Programmi\EasyPHP 3.0\www\php-test\index.php on line 46

Why? I know I *should* set those two variables ($SERVER_NAME, $SCRIPT_NAME).. but, how?Maybe it should be like this..

$pt2="http://$_SERVER[SERVER_NAME]$_SERVER[SCRIPT_NAME]";

?

Link to comment
Share on other sites

Do you have the $_GLOBALS array available?

<?php$pt2="http://$SERVER_NAME$SCRIPT_NAME";echo '$SERVER_NAME :  ' . $SERVER_NAME . '<br />';echo '$SCRIPT_NAME :  ' . $SCRIPT_NAME . '<br />';echo $pt2 . '<br />';$pt2= substr($pt2,0,strlen($pt2)-21);echo $pt2 . '<br />';echo '<pre>';print_r( $GLOBALS );echo '</pre>';?>

Try that script to see what your output is. $SERVER and $SCRIPT_NAME are listed a $_GLOBALS on my output. (As well as in the $_SERVER array.)

Link to comment
Share on other sites

Using that code I get this output.

Notice: Undefined variable: SERVER_NAME in C:\Programmi\EasyPHP 3.0\www\php-test\test.php on line 3Notice: Undefined variable: SCRIPT_NAME in C:\Programmi\EasyPHP 3.0\www\php-test\test.php on line 3Notice: Undefined variable: SERVER_NAME in C:\Programmi\EasyPHP 3.0\www\php-test\test.php on line 5$SERVER_NAME :Notice: Undefined variable: SCRIPT_NAME in C:\Programmi\EasyPHP 3.0\www\php-test\test.php on line 6$SCRIPT_NAME :http://Array( [GLOBALS] => Array *RECURSION* [_POST] => Array ( ) [_GET] => Array ( ) [_COOKIE] => Array ( [visits] => 56 ) [_FILES] => Array ( ) [pt2] => )
Link to comment
Share on other sites

Maybe it should be like this..
$pt2="http://$_SERVER[SERVER_NAME]$_SERVER[SCRIPT_NAME]";

?

Close enough... it's more like:
$pt2="http://{$_SERVER['SERVER_NAME']}{$_SERVER['SCRIPT_NAME']}";

I don't see why would you want a code that stripts the last 21 characters from a URL though... still, I believe that is what this code is supposed to do. Where did you see it from anyway?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...