Jump to content

Storing User Log In Details


CiaranAshton

Recommended Posts

Hi im new to this site. Been coding PHP a little while but still learning. Im writing a simple log in feature to make a website more dynamic.

 

My question is I've written a simple html form and simple php admin page. If I link to another page that I wish for only the admin to see, or back to the original admin page from one of the others It comes up with errors as it wants me to log in again. So I wold like to knwo how to carry the details of the login from one page to another. Im trying to keep it simple as I want to make sure I understand how it all works.

 

 

Below is the code I wrote for my admin page

<Html>
<head>
<title>Admin Page</title>
</head>
<body>



<?Php
include 'config.php';
include 'opendb.php';


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


$level = mysql_result(mysql_query("SELECT level FROM users WHERE 


username='".mysql_real_escape_String($username)."' 


AND password='".mysql_real_escape_String($password)."'"),0) or die        
('<br><br><br><br><b>Incorrect Password or Username');


switch ($level)
{
case 1: echo ("Hello Admin");break; 
case 2: echo ("Hello User");break; 
}

?>
</body>
</html>

 

Thank you, Ciaran.

Link to comment
https://forums.phpfreaks.com/topic/272182-storing-user-log-in-details/
Share on other sites

What your looking for is a form of what's called "Persistance" and is normaly accomplished by using the built in PHP super-global array $_SESSION.

 

To make this work you must start every page that depends on the user being logged in with

<?php
session_start();
...
?>

you can then pass information between pages using

$_SESSION['your_variable'] = $theVariableToBePassed;

to set the session variable and

$variableThatHasBeenPassed = $_SESSION['your_variable'];

to get it

 

The normal rules apply fore case sensetivity and the such.

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.