-3

[[Fixed]] Was accessing the file directly in my browser, instead of through the localhost web server

--

I'm fairly new to PHP and trying to get the following to work.

This code is in the middle of a HTML page.

I've initialized and set the variable right above the if statement

I'm checking if the variable is '1', when I've set it to '3'.

My problem is, that it is still running the html code in the IF statement when it shouldn't be

Any idea's?

Thanks in advance

<?php
  $ad='3';
  if($ad=='1')
    {?>
    <p>Broken</p>
    <?php
  }
?>
  • 5
    You set `$_SESSION['test']` but are checking `$_SESSION`. – Dissident Rage Apr 02 '14 at 12:01
  • 1
    It is working correctly. I think you have got error in other part.Please post the full code – krishna Apr 02 '14 at 12:09
  • Create a variable `$test` to receive the `$_SESSION` like `$test = $_SESSION['test']`, then test with the variable instead of the $_SESSION itself – Michel Ayres Apr 02 '14 at 12:12
  • I [tested your code](http://sandbox.onlinephpfunctions.com/code/c1ae4c678523dc53c13838d509b264afbe056771) and it's working – Michel Ayres Apr 02 '14 at 12:17
  • @user3489221 Look at this ... http://sandbox.onlinephpfunctions.com/code/4989e08557f84bcaae3be584e548066f7c5f23b2 It's a copy and paste of your code, and it seems to work. Could you provide more info about it? – Michel Ayres Apr 02 '14 at 12:30
  • Take a look at this: [Alternative syntax for control structures](http://php.net/manual/en/control-structures.alternative-syntax.php), [Can HTML be embedded inside PHP “if” statement?](http://stackoverflow.com/questions/722379/can-html-be-embedded-inside-php-if-statement), [How do I add PHP code to .html files?](http://stackoverflow.com/questions/11312316/how-do-i-add-php-code-to-html-files) – Michel Ayres Apr 02 '14 at 12:35
  • I copied the code from that sandbox into a fresh html file, i get both HTML outputs. It works on that site, but not in reality – user3489221 Apr 02 '14 at 12:40
  • @user3489221 you really created html file and pasted php code in that ? – krishna Apr 02 '14 at 12:54
  • 1
    Issue was viewing the files in browser from file directory, instead of running through the web server. Working as expected not. Amateur mistake. Thanks for all the help – user3489221 Apr 02 '14 at 13:13

3 Answers3

0

You have to check the test field inside $_SESSION array! You are checking the whole array... :)

<?php
session_start();
$_SESSION['test']='2';

if($_SESSION['test']=='1')
{?>
   <!--HTML Code-->
<?php
}
?>
rvandoni
  • 3,297
  • 4
  • 32
  • 46
  • Thanks for the reply. Unfortunately, this was a typo on here, and I have the full variable name in my code. Have updated OP – user3489221 Apr 02 '14 at 12:06
0

You have also to check if the $_SESSION var is set:

   session_start(); 

    if(isset($_SESSION['test']))
    {
        if($_SESSION['test']=='1')
        {
          //do stuff HTML code
        }
        elseif($_SESSION['test']=='2')
        {
          //do other stuff HTML code
        }

    }
Rubensito
  • 102
  • 6
  • Thanks for the reply. Unfortunately, I have that code already, I just didn't realize it was needed for the statement to work so left it out of this post. Have updated OP – user3489221 Apr 02 '14 at 12:11
0

I found the problem.

You have created a HTML file and using php inside that which is totally wrong .

To get php code working you need to create a php file and write php code

create a php file by saving a file as filename.php and copy those contents there, which will work fine

Also make sure that you have php installed on you system.If not download and install wamp server

krishna
  • 4,069
  • 2
  • 29
  • 56