Jump to content

[SOLVED] How to avoid entry in website without login.


shruti

Recommended Posts

Hello All,

 

I am a new member. I have a small website. I am in process of learning PHP and making something.

 

I have made php login and registration and a home page.

 

But the problem here is that if you bookmark the homepage then anyone can see details without login.

 

What can I include on each page so that no one can enter my pages unless they login??

 

 

My login code is here:

 

<?php

 

$host = "localhost";

$user = "sssi";

$pass = "sss";

$dbname = "xxx";

 

$connection = mysql_connect("localhost", "root", "") or die("Cannot connect to MySQL server: " . mysql_error());

$db_selected = mysql_select_db('xxx', $connection);

 

session_start();

$username = $_POST['username'];

$password = ($_POST['password']);

 

$query = "select * from users where username = '$username' and password = '$password'";

 

$result = mysql_query($query);

 

if (mysql_num_rows($result) != 1) {

$error = "Bad Login";

    include "login.html";

 

} else {

    $_SESSION['username'] = "$username";

    header("location:main.html");

}

 

?>

 

 

 

Please help me with code.....what do i do?

 

login.php

<?php
session_start();
$host = "localhost"; 
$user = "sssi"; 
$pass = "sss"; 
$dbname = "xxx"; 

$connection = mysql_connect("localhost", "root", "") or die("Cannot connect to MySQL server: " . mysql_error());
$db_selected = mysql_select_db('xxx', $connection);

$username = $_POST['username'];
$password = ($_POST['password']);

$query = "select * from users where username = '$username' and password = '$password'";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
   $error = "Bad Login";
   include "login.html";

} else {
   $_SESSION['username'] = $username;
    header("location:main.html");
}

?>


 

homepage.php

<?php 
session_start();

if(!isset($_SESSION['username'])) {
  include "login.html";exit;
}

//rest of home page code....
?>

Hello All,

 

I am a new member. I have a small website. I am in process of learning PHP and making something.

 

I have made php login and registration and a home page.

 

But the problem here is that if you bookmark the homepage then anyone can see details without login.

 

What can I include on each page so that no one can enter my pages unless they login??

 

 

My login code is here:

 

<?php

 

$host = "localhost";

$user = "sssi";

$pass = "sss";

$dbname = "xxx";

 

$connection = mysql_connect("localhost", "root", "") or die("Cannot connect to MySQL server: " . mysql_error());

$db_selected = mysql_select_db('xxx', $connection);

 

session_start();

$username = $_POST['username'];

$password = ($_POST['password']);

 

$query = "select * from users where username = '$username' and password = '$password'";

 

$result = mysql_query($query);

 

if (mysql_num_rows($result) != 1) {

$error = "Bad Login";

    include "login.html";

 

} else {

    $_SESSION['username'] = "$username";

     header("location:main.html");

}

 

?>

 

 

 

Please help me with code.....what do i do?

 

 

because your homepage is not protected!!

 

change to the following:

 

header("Location: main.php");

 

then in main.php have the following code

 

session_start();

 

if(!isset($_SESSION['username']))

{

//they bookmarked the page and didnt use the standard login

//redirects them to login page

header("Location: login.html");

exit;

}

else

{

//success!!! they logged in and have started a previous session

}

It redirects me to the Login page but also give this error:

 

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\WAMP\www\tmetrix\try.php:12) in C:\WAMP\www\tmetrix\try.php on line 160

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\WAMP\www\tmetrix\try.php:12) in C:\WAMP\www\tmetrix\try.php on line 160

 

 

What should i do?????

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.