Jump to content

Variables, ifs, else ifs, with echos.


azpyroguy

Recommended Posts

This is a landing page that gets displayed when a user connects to our VPN server (page is hosted on a Ubuntu 10.4 server)

 

The idea behind this is having this script which is embedded inside of a HTML document (that part works), and do get the "YOUR IP ADDRESS IS" to work without the "ifs and else". 

 

The function of this script is simple, set a variable of the users currently assigned IP by the VPN, then check based on that variable, if they are assigned a 10.65.x.x IP they get a "CONNECTED" echoed in the box, and if any other IP is found, or not found the receive a "FAILED" message in the box, and then a pop up html message that provides them information about why their connection has failed.

 

I am having some trouble getting this to run properly.. I get no errors, from I can see, but no page display either...

 

<?$VPNremote_ip = "REMOTE_ADDR"; ?>

        <div class="lowerMessage" style="MARGIN: 10px auto; WIDTH: 400px">

<p align="left">

        <? if (VPNremote_ip == "10.65") echo SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p>

<p align="left">;

        else if echo SSL CONNECTION STATUS - <span style="COLOR: #cc0000; FONT-SIZE: 14px; FONT-WEIGHT: bold">FAILED</span>

 

                        <script type="text/javascript">

                                openWin('not_connected.html');

                        </script>

?>

                <br ><br >

                YOUR IP ADDRESS IS: <b><? echo getenv('REMOTE_ADDR'); ?></b></p>

 

 

Anyone got any ideas on what I am doing wrong here...

 

I have attached a screen grab, showing on the script is working.. sort of...

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/205787-variables-ifs-else-ifs-with-echos/
Share on other sites

May not be correct, but from what I see in the code, it looks like you're saying if their IP address is EXACTLY 10.65, then show them connected ...

 

Wouldn't it need to be if ($remote_addr like 10.65%) { ...etc ?

 

<?php

$VPNremote_ip = "REMOTE_ADDR"; 

?>
        <div class="lowerMessage" style="MARGIN: 10px auto; WIDTH: 400px">
<p align="left">

<?php

if (VPNremote_ip == "10.65") 
{

?>
SSL CONNECTION STATUS- <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p>
<p align="left">

<?php
}
else 
{
?>

SSL CONNECTION STATUS - <span style="COLOR: #cc0000; FONT-SIZE: 14px; FONT-WEIGHT: bold">FAILED</span>

<?php
}
                        <script type="text/javascript">
                                openWin('not_connected.html');
                        </script>
                <br ><br >
                YOUR IP ADDRESS IS: <b><?php echo getenv('REMOTE_ADDR'); ?></b></p>

A few things wrong with your code.  Blank page most of the time if not always indicates that you have errors in your PHP coding.  You need to turn on error checking your scripts when you write them.  Also you should use logical scripting so that people can read your code and help you. 

<?PHP

 

IF CONDITIONS {

  DO SOMETHING HERE

}

ELSE  {

  DO THIS

 

}

 

?>

On your if statement which I didn't change to what I am going to suggest but.  If you want that portion to work I would use string positive:

 

E.G.

 

<?php
$mystring = "REMOTE_ADDR";
$findme   = '10.65.';
$pos = strpos($, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?> 

 

It should always be like:

 

 

<?php

$VPNremote_ip = "REMOTE_ADDR"; 

?>


		<div class="lowerMessage" style="MARGIN: 10px auto; WIDTH: 400px">
		<p align="left">
<?php 

if (VPNremote_ip == "10.65") {
   echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p><p>align="left">';
}

else { 
   echo 'SSL CONNECTION STATUS - <span style="COLOR: #cc0000; FONT-SIZE: 14px; FONT-WEIGHT: bold">FAILED</span>';
}
?>
		<script type="text/javascript">';
		openWin('not_connected.html');
		</script>

                	<br ><br >
                	YOUR IP ADDRESS IS: <b><? echo getenv('REMOTE_ADDR'); ?></b></p>

I was trying to correct it in my code paste as well, but it didn't look as nice as yours :D

 

 

Oh no worries.  I was just letting you know that it makes it easier if you write it both for others and for your self.  Hard to see the problem and unless you follow logically. 

 

 

PS. You were missing  "" and other things that prevented your code from running.  Look up turning on error reporting scripts it helps.  Like missing T_STRING would point you to say a missing ' or " or ; and the line that its having issues with.  Always good practice when having issues to turn on error reporting.

 

Thanks,

A few things wrong with your code.  Blank page most of the time if not always indicates that you have errors in your PHP coding.  You need to turn on error checking your scripts when you write them.  Also you should use logical scripting so that people can read your code and help you. 

<?PHP

 

IF CONDITIONS {

  DO SOMETHING HERE

}

ELSE  {

  DO THIS

 

}

 

?>

On your if statement which I didn't change to what I am going to suggest but.  If you want that portion to work I would use string positive:

 

E.G.

 

<?php
$mystring = "REMOTE_ADDR";
$findme   = '10.65.';
$pos = strpos($mystring , $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?> 

 

It should always be like:

 

 

<?php

$VPNremote_ip = "REMOTE_ADDR"; 

?>


		<div class="lowerMessage" style="MARGIN: 10px auto; WIDTH: 400px">
		<p align="left">
<?php 

if (VPNremote_ip == "10.65") {
   echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p><p>align="left">';
}

else { 
   echo 'SSL CONNECTION STATUS - <span style="COLOR: #cc0000; FONT-SIZE: 14px; FONT-WEIGHT: bold">FAILED</span>';
}
?>
		<script type="text/javascript">';
		openWin('not_connected.html');
		</script>

                	<br ><br >
                	YOUR IP ADDRESS IS: <b><? echo getenv('REMOTE_ADDR'); ?></b></p>

 

Let me know if you get it working... :)

I think its close...

 

I am now getting the echo of the failed message properly.

 

I even tried to place these to try and get the success message to appear... so it acts like its not receiving the proper variable

 

$VPNremote_ip = $_SERVER['REMOTE_ADDR'];

 

if (VPNremote_ip == "10.12%") {

  echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p><p>align="left">';

}

 

or

$VPNremote_ip = $_SERVER['REMOTE_ADDR'];

 

if (VPNremote_ip == "my.ip.address.here") {

  echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p><p>align="left">';

}

 

 

 

 

 

Tried this and it works.  Another best practice is to echo out dependent varibles like:

echo 'VPN REMOTE IP ADDRESS IS:' .$VPNremote_ip . '</br>';  //check to see if the $_SERVER['REMOTE_ADDR'] is pulling an ip

 

<?php

$VPNremote_ip = $_SERVER['REMOTE_ADDR'];
echo 'VPN REMOTE IP ADDRESS IS:' .$VPNremote_ip . '</br>';  //check to see if the $_SERVER['REMOTE_ADDR'] is pulling an ip

$validip = '10.0'; //This is a valid ip range
$pos = strpos($VPNremote_ip, $validip);

if ($pos !== false) {
   echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold" align="left">CONNECTED</span></p><p>';
}else {
echo 'SSL CONNECTION STATUS - <span style="COLOR: RED; FONT-SIZE: 14px; FONT-WEIGHT: bold" align="left">NOT CONNECTED</span></p><p>';
}


?>

[img][/img]

I think its close...

 

I am now getting the echo of the failed message properly.

 

I even tried to place these to try and get the success message to appear... so it acts like its not receiving the proper variable

 

$VPNremote_ip = $_SERVER['REMOTE_ADDR'];

 

if (VPNremote_ip == "10.12%") {

  echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p><p>align="left">';

}

 

or

$VPNremote_ip = $_SERVER['REMOTE_ADDR'];

 

if (VPNremote_ip == "my.ip.address.here") {

  echo 'SSL CONNECTION STATUS - <span style="COLOR: #000099; FONT-SIZE: 14px; FONT-WEIGHT: bold">CONNECTED</span> </p><p>align="left">';

}

 

I noticed you are forgetting to place $ in front of your varibles in your if statements too.

 

 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.