User levels with PHP - php

What I'm trying to do is make it so that when a specific user level is set in my Database it shows up specific text. Here is my code.
$userget = mysql_query("SELECT * FROM `usertable` WHERE `username`='".$_SESSION['user']."'");
$user = mysql_fetch_array($userget);
function getUserlevel() {
if($user['userlevel'] == '1') { echo "Regular User"; }
elseif($user['userlevel'] == '2') { echo "Moderator"; }
elseif($user['userlevel'] == '3') { echo "Administrator"; }
else { echo "Undefined"; }
}
I know the function is working, because the Echo of Undefined works, which I set up as a test but the others are not working despite my user level being set to 3 and only echoing Undefined. A session is also set for when the user is logged in.
This is when the function is called.
<div class="userData">Welcome Back, <?php echo $_SESSION['user']; ?><hr /><?php
getUserlevel(); ?></div>

Ok, I'm going to take a stab at:
getUserlevel($user['userlevel']);
function getUserlevel($usrlvl) {
if($usrlvl == '1') { echo "Regular User"; }
elseif($usrlvl == '2') { echo "Moderator"; }
elseif($usrlvl == '3') { echo "Administrator"; }
else { echo "Undefined"; }
}

I just included query into function, and its working :P
<?php
function getUserlevel() {
$userget = mysql_query("SELECT * FROM `users` WHERE `username`='".$_SESSION['username']."'");
$user = mysql_fetch_array($userget);
if($user['user_level'] == 1) { echo "Regular User"; }
elseif($user['user_level'] == 2) { echo "Moderator"; }
elseif($user['user_level'] == 3) { echo "Administrator"; }
else { echo "Error"; }
}
?>

You can not access $user as $user[userlevel] because mysql_fetch_array return an array. Does username is unique if not use an id instead of using username. So you should write the function as below
function getUserlevel()
{
foreach($user as $u)
{
if($u['userlevel'] == 1){echo "Regular User";}
else if($u['userlevel'] == 2){echo "Moderator";}
else if($u['userlevel'] == 3){echo "Administrator";}
else{echo "Undefined";}
}
}
If it does not work make sure you have write a vaild mysql query

Related

Weird PHP responses in subdomain

I am trying to make a website that echos out a number or a word based on some conditions. I connected it to my database, but it always echos out 2 (user not found), instead of yes100 (password and username correct).
The weird thing is, it works on my main domain, where it outputs yes100, but here it just can not do that for some reason.
I am sure my database details are correct, and I have uploaded the file where it should be.
This is my code (not secure at all, but it is for personal use only.)
$result = $link->query($sql);
if ($result->num_rows > 0) {
// Outputting the rows
while($row = $result->fetch_assoc())
{
$password = $row['password'];
$salt = $row['salt'];
$plain_pass = $_GET['password'];
$stored_pass = md5(md5($salt).md5($plain_pass));
function Redirect($url, $permanent = false)
{
if (headers_sent() === false)
{
header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
}
exit();
}
if($stored_pass != $row['password'])
{
echo "BLAHAHAHAHAHAHAHAHA";
exit();
}
else
{
echo "yes"; // Correct pass
}
if (strlen($row['hwid']) > 1)
{
if ($hwid != $row['hwid'])
{
echo "0"; // Wrong
}
else
{
echo "100"; // Correct
}
}
else
{
$sql = "UPDATE ". $tables ." SET hwid='$hwid' WHERE username='$user'";
if(mysqli_query($link, $sql))
{
echo "rdy"; // HWID Set
exit();
}
else
{
echo "4"; // Else errors
exit();
}
}
}
}
else
{
echo "2"; // User doesn't exist
exit();
}
?>
I forgot to give the user the permissions. It works now. Thanks everyone.

Login form db check error

Hi i need help to make a login check thourght db working anyone can say to me where is the error?
This is the code
if (!empty($_POST['user']) && !empty($_POST['password']))
{
$user=stripslashes(trim($_POST['user']));
$password=stripslashes(trim($_POST['password']));
mysql_connect("localhost","root","");
mysql_select_db("project");
$check=mysql_query("SELECT * FROM utenti WHERE nome='$user' AND password='$password'");
if(mysql_num_rows($check)!0)
{
$details=mysql_fetch_array($check);
$_SESSION['display_name']=$details[0];
$_SESSION['username']=$details[1];
$_SESSION['password']=$details[2];
print "Login succesful. <p> Level access: " . $details["type"] ;
}
else
{
print "Error";
}
}
else
{
print "Not all fields are compiled" ;
}
if ($details["type"] == "admin" )
{
$admn = 1;
}
else
{
$admn = 0;
}
I've no clue why is not working. Thanks in advance.
Change this:
if(mysql_num_rows($check)!0)
To This:
if(mysql_num_rows($check) != 0)

PHP $_SESSION returning incorrect value

Ok, so when I execute the initial function it works fine, the username gets stored in the database, however when I run the second function that appends the username to the text the user chooses to enter the IF statement returns 'no user' - when a user is defined...
If anyone knows how to fix this that would be great - I am currently learning PHP and mysql so I am sorry if any of this is incorrect
<?php
session_start()
// connect to the database
mysql_connect("localhost", "root", "");
mysql_select_db("ajaxchat");
// read the stage
$stage = $_POST['stage'];
// primary code
if($stage == 'initial') {
// check the username
$user = $_POST['user'];
$query = mysql_query("SELECT * FROM chat_active WHERE user='$user'");
if (mysql_num_rows($query) == 0) {
$time = time();
//
mysql_query("INSERT INTO chat_active VALUES ('$user', '$time')");
// set the session
$_SESSION['user'] = $user;
echo 'good';
}
else {
echo 'taken';
}
}
/////////////// PROBLEM FUNCTION ///////////////
================================================
else if($stage == 'send') {
// get the textdomain
$text = $_POST['text'];
// check for user_error
if (isset($_SESSION['user'])) {
$user = $_SESSION['user'];
echo $user.' - '.$text.'<br />';
}
else {
echo 'no user';
}
}
else {
echo 'error';
}
?>
This is the javascript:
<script type="text/javascript">
function chat_initialise() {
var user = document.getElementById("chat_user").value;
$.post("./chat.php", {stage:"initial", user:user}, function(data) {
if (data == "good") {
$('#initial').css('display', 'none');
$('#content').css('display', 'inline')
}
else {
alert("That username is taken! Please try another.");
}
});
}
function chat_send() {
var text = document.getElementById("chat_text").value;
$.post("./chat.php", {stage:"send", text:text}, function(data) {
document.getElementById("chat_text").value = '';
$('#window').text($('#window').text() + data);
// alert(data)
});
}
</script>
I fixed it - changed the POST function to take the current username then redefine it as a variable in the second function:
else if($stage == 'send') {
// get the textdomain
$text = $_POST['text'];
$user = $_POST['user'];
echo $user;
// check for user_error
if (isset($_SESSION['user'])) {
$_SESSION['user'] = $user;
echo $user.' - '.$text.'<br />';
}
else {
echo 'no user';
var_dump($_SESSION);
}
}
Thanks for all your help guys!!

Checking session for empty

I am using the codeigniter framework. I have session set in $this->session->userdata('sess_place') but I cannot echo using if condition. Maybe my if condition is wrong?
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl = TRUE) {
echo $pl;
}
else {
echo "Select your city";
}
You have used assign operater(=) instead of ==, which is wrong. Try this:
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl == true) // and also no need of isset($pl) here jusy if($pl == TRUE) is enough
{
echo $pl;
}
else
{
echo "Select your city";
}
This should work for you:
$pl = $this->session->userdata('sess_place');
if(!empty($pl)) {
echo $pl;
} else {
echo "Select your city";
}
Try to fetch data from the session. If it returns something other than false, it must be set.Try with -
if ($this->session->userdata('sess_place')) {
echo $this->session->userdata('sess_place');
} else {
echo "Select your city";
}
If you storing Boolean value in session then try this.
$pl = $this->session->userdata('sess_place');
if(isset($pl) && $pl === true) // this checks value and its data type
{
echo "Got it";
}
else
{
echo "Select your city";
}

Header location issue. Cant seem to find the issue in if else statement

I have some issues with the following PHP code:
require '../core/connection.php';
require '../includes/functions.php';
if (isset($_SESSION['user_id'])) {
if (isset($_POST['current_user_password'])) {
$current_user_password = md5($_POST['current_user_password']);
if ($current_user_password != $_SESSION['user_password']) {
header('Location:../edit_credentials.php?edit=password&error=current_password');
} else {
$new_password = $_POST['new_user_password'];
$new_password2 = $_POST['new_user_password2'];
if ($new_password == '' || ($new_password2 == '') {
header('Location:../edit_credentials.php?edit=password&error=new_password_empty');
}
if ($new_password != $new_password2) {
header('Location:../edit_credentials.php?edit=password&error=new_password_not_equal');
} else {
$new_password = md5($new_password);
edit_user_password($user_id,$new_password);
}
}
} else {
echo 'current_user_password not set';
}
} else {
echo 'Session not set';
}
Here is the function as well:
function edit_user_password($user_id,$user_password){
global $db;
$user_password_data = $db->query('
UPDATE `users`
SET `user_password` = "'.$user_password.'" WHERE `user_id` = "'.$user_id.'";');
$_SESSION['user_password'] = $user_password;
$db->query($user_password_data);
header("Location:../edit_credentials.php?saved=password");
}
Everything works besides this part:
if ($new_password == '' || ($new_password2 == '') {
header('Location:../edit_credentials.php?edit=password&error=new_password_empty');
}
When it comes to this part, where the passwords are empty, if i switch out the redirection with a die('test'), it works, but it wont work with the redirect. Any chance you guys know why this is not working?
Thanks in advance for all help.
You should add an exit; after each header('Location:...'); call.
Otherwise the script continues to run.

Categories