-4

I am new to PHP and I am trying to have some code, that what a user is logged in, it will display the users name, and when they are not logged in, it will display Login/Register. I have some code, that I think should work, but it keeps displaying php on my website.

Here is the code:

           <?php
        $username = "Not telling";
        $password = "Not telling";
        $hostname = "127.0.0.1";


        $dbh = mysql_connect($hostname, $username, $password) or die("Could not connect to database");
        $selected = mysql_select_db("Not telling", $dbh);

        $username = $_COOKIE['USER'];
        $query = "SELECT name FROM Accounts WHERE username ='$username'";
        $result = mysql_query($query);
        $count = mysql_num_rows($result);
        $name = mysql_result($result, 1);

        if(isset($_COOKIE['USER'])) {
            echo '<a href="">' . $name . '</a>';
        }else{
            echo "<a href='login.php'>Login / Register</a>";
        }
        mysql_close($dbh);
        ?>

Here is what is displayed on my website: http://prntscr.com/6rdrgj

Thanks in advance!

Avery246813579
  • 199
  • 1
  • 11
  • Obligatory your code has an [SQL injection vulnerability](https://xkcd.com/327/) and [mysql_* functions are deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) comment. – Alexander O'Mara Apr 08 '15 at 23:03
  • **A:** Install a webserver which includes PHP. Xampp, Wamp, Mamp. Google those. – Funk Forty Niner Apr 08 '15 at 23:47

1 Answers1

0

(1) check for whether php module is installed:

### redhat family
$ rpm -q php

### debian family
$ dpkg -s php5

judging from the code it also looks like you need php-mysql or php5-mysql installed.

(2) check for short_open_tag in your php.ini.

(3) Also check for apache (or whatever webserver you're using) to include php module and settings to associate .php file with PHP module...

(4) or use php in CGI mode and insert at the top of your script #!/usr/bin/php and set executable bit chmod +x myscript.php, then move it into dedicated CGI directory like /var/www/cgi-bin.

Droopy4096
  • 311
  • 1
  • 10