Jump to content

Recommended Posts

Goal

assign user level into a session when a user logs in.

 

login mysql code:

// Verify
		$email = mysql_escape_string($_POST['email']);
		$password = md5($_POST['password']);

		$gUser = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error());
		$verify = mysql_num_rows($gUser);

		if($verify > 0){
			echo '<h2>Login Complete</h2>';

		}else{
			echo '<h2>Login Failed</h2>
			      <p>Sorry your login credentials are incorrect.<br /><br />';
		}

 

So what I need to do is assign a level variable,

This is what I'm thinking.

$level = (a db query) [-the level will be between 1 and 5-]

then assign it to a session var

$_SESSION["level"] = $level

 

So I am looking to learn a reliable way to set this variable and have it stick as a session.

Its not working.

 

Heres what the entire script looks like.

<?php
if(isset($_SESSION["level"])) {			
            echo '<h3>Admin</h3>
            <p>This is an example of a content page!</p>';
		echo '<h3>Welcome '.$_SESSION["name"].', your user level is '.$_SESSION["level"];
} else {
	  include('db_info.php');
	connectDB();
	selectDB("-----");

	if(isset($_POST['email']) && isset($_POST['password'])){
		// Verify
		$email = mysql_escape_string($_POST['email']);
		$password = md5($_POST['password']);

		$gUser = mysql_query("SELECT * FROM users WHERE email='".$email."' AND password='".$password."' LIMIT 1") or die(mysql_error());
		$verify = mysql_num_rows($gUser);

		if($verify > 0){
			/*Set user level*/
			$row = mysql_fetch_assoc($gUser);
			$_SESSION["level"] = $_row["level"];
			$_SESSION["name"] = $email;
			/*End of set user level*/
			echo '<h2>Login Complete</h2>';
			echo '<h3>Welcome '.$_SESSION["name"].', your user level is '.$_SESSION["level"];
		}else{ /*	If login fails> */
			echo '<h2>Login Failed</h2>
			      <p>Sorry your login credentials are incorrect. <a href=\"admin.php\">Try again</a><br /><br />';
		}
	}else{
	?>

 

heres what my db looks like

$sql ="CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL,
  email VARCHAR(50) NOT NULL,
  password VARCHAR(32) NOT NULL,
  date DATE NOT NULL,
  level INT NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.