Cory
Cory

Reputation: 145

Page only accessible when I log in with PHP


I have been working on a extensive idea/project and I am almost finished. I created a registration database and another database that holds the information that I am trying to access after I log in. The one problem I am having is making my html/php website only accessible by logging in. I tried

 <?php
    if(!isset($_SESSION['username'])) {
    die("Please login");
}

?>

but it's not working. This is my code that is used to log in.

<?php

         error_reporting(E_ALL);
         ini_set('display_errors', 'on');
         if(isset($_POST["submit"]))
        {
        $User = "****"; 
        $Password = "****";
        $Database = "****";
        $Host = "localhost"; 
       $con = mysqli_connect($Host, $User, $Password);
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        mysqli_select_db($con,$Database);
        $myusername = $_POST["username"];
        $mypassword = $_POST["password"];
        $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
        mysqli_query($con,$sql);
        mysqli_commit($con);
        mysqli_close($con);

        echo file_get_contents('main_login.php');
        }
else
        {
        ?>
        <body style='background: url(https://peltjournal.files.wordpress.com/2013/10/bigstock-blank-chalkboard-109848.jpg);'>;

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<form name="form1" method="post" action="proInput.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Registeration </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input type="Text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="Text" name="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="Submit" name="submit" value="Register"></td>
</tr>
</form>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><button id="myButton" class="float-left submit-button">Already a member?</button></td>

        <script type="text/javascript">
            document.getElementById("myButton").onclick = function () {
            location.href = "";
        };
        </script>
        </tr>
</table>
</td>


</tr>

</table>        

        </div>
        <?
        }

?>

Here is the page that I only want accessed by logging in.

 <?php
    if(!isset($_SESSION['username'])) {
    die("Please login");
}

?>
<html>
<head>
</head>
<body>
<body style='background: url(https://peltjournal.files.wordpress.com/2013/10/bigstock-blank-chalkboard-109848.jpg);'>;
<table width="175" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<form name="form1" method="post" action="proInput.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Classes:</strong></td>
<td><select name="menu1" id="menu1">
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select></td>
<script type="text/javascript">
 var urlmenu = document.getElementById( 'menu1' );
 urlmenu.onchange = function() {
      window.open( this.options[ this.selectedIndex ].value );
 };
</script>
</tr>
<tr>
<td><button onclick="goBack()">Logout</button></td>

<script>
function goBack() {
    window.history.back();
}
</script>
</tr>
</table>

</body>
</html>

Upvotes: 0

Views: 130

Answers (2)

Ajay Makwana
Ajay Makwana

Reputation: 2372

Try this..

session_start();
if (($_SESSION['username'] == '') || (!isset($_SESSION['username'])) {
    die("Please login");
}

Upvotes: 0

Lukasz
Lukasz

Reputation: 409

 <?php
    if(!isset($_SESSION['username'])) {
    die("Please login");
}
    else {
    echo "what user can see";
}
?>

Upvotes: 1

Related Questions