Jump to content

Recommended Posts

for some reason my session variables dont stay when i use require once on login_success.php and i get these errors. everything works properly when i use them on the checklogin.php but when i try to use them on other pages they dont.

 

 

Notice: Undefined index: myusername in C:\wamp\www\login\checklogin.php on line 13

 

Notice: Undefined index: mypassword in C:\wamp\www\login\checklogin.php on line 14

Wrong Username or Password

Notice: Undefined variable: _SESSION in C:\wamp\www\login\login_success.php on line 5

 

checklogin.php

<?php
$host="localhost"; //Host name 
$username="ryan"; // Mysql username 
$password="password"; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$sql="SELECT username and password FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
$accesslevels = mysql_query("SELECT accesslevel FROM $tbl_name WHERE username='$myusername' and password ='$mypassword'");
$accesslevel = mysql_fetch_assoc($accesslevels); 


// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

//register user and accesslevel
if($count==1 and $accesslevel['accesslevel']=='1'){
session_start(); 
$_SESSION['user'] = $myusername;
$_SESSION['accesslevel'] = $accesslevel['accesslevel'];
header("location:login_success.php");
}
elseif($count==1 and $accesslevel['accesslevel']=='2'){
session_start(); 
$_SESSION['user'] = $myusername;
print $_SESSION['user'];
echo "welcome manager"; 
}
elseif($count==1 and $accesslevel['accesslevel']=='3'){
session_start(); 
$_SESSION['user'] = $myusername;
print $_SESSION['user'];
echo "welcome admin"; 
}
else {
echo "Wrong Username or Password";
}
?>

 

 

login_success.php

<?php
// Check if session is registered
require_once 'checklogin.php';
print $_SESSION['user'];
?>

Link to comment
https://forums.phpfreaks.com/topic/180353-solved-global-session/
Share on other sites

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.