I am using a simple PHP/MYSQL login system using sessions and I need to print the username for the person currently logged in to the session.
The current code I am using to do this is:
<?php
session_start();
include_once 'dbconnect.php';
$res=mysql_query("SELECT username FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_row($res);
print_r($userRow)
?>
But the output of this is:
Array ( [0] => USERNAME )
While I would like it to be:
USERNAME
How would I achieve this, or is this possible?
Thank you in advance!
The value is in array. you have to access the index to get the value like this,
echo $userRow['username'];
I would suggest to store the username in session and access it directly instead of accessing from database everytime.
$_SESSION['username'] = "name here";
echo $_SESSION['username'];
EDIT
echo $userRow[0];
this should help you.
echo $userRow['username'];
Try this out.
Related
<?php
session_start();
if(!isset($_SESSION['myusername'])) {
header('location:kursna_login.php');
}
?>
Code always redirect me to kursna_login.php when the username and pass are correct in the login form. Is this the right declaration of the code?
if you are redirected to "kursna_login.php" so the session variable is not set , once you store the username in a session variable , for example let's suppose the username is coming from a form with a post method
you should save iy like this
$username = $_POST['username'];
$_SESSION['myusername'] = $username;
and after this you have to checkk the session array eith a simple print_r like this
<?php
session_start();
echo "<pre>";
print_r($_SESSION);
?>
this would give you all the session variables setted in your app as key => value and then try to verify if the name is really setted in session global variiable or not .
I downloaded a php login source and now it works and it even logs in. when it logs in it shows your name with
<?php echo $_SESSION['username']; ?>
but i also want to display there balance by putting
<?php echo $_SESSION['balance']; ?>
But it doesnt display the balance?
https://gyazo.com/7cd0a7888ac976590391888925d0c18f
I added the balance table to the existing tables.
I really dont know what to do! :(
Please Insert;
<?php session_start(); ?>
In every page where you want to access $_SESSION global array.
in your case;
<?php session_start(); ?>
$_SESSION['balance'] = 1000;
echo $_SESSION['balance']; // will output 1000.
suppose this page was set-session-value.php and you want to get this $_SESSION['balance'] value in get-session-value.php do as follow in get-session-value.php file;
<?php session_start(); ?>
echo $_SESSION['balance'];
In your login.php add this line after setting session for username
$_SESSION["balance"]=$balance;
And in your home, it is always advised to check if session exists and then print it. So,
if(isset($_SESSION["balance"])){echo $_SESSION["balance"]; }
start session before use
<?php
session_start();//start session
$_SESSION['username']='abc';//Set value session
$_SESSION['balance']=10;
echo $_SESSION['username']; //Use value of session
echo $_SESSION['balance'];
?>
please use this process for session use
When you logged into your application.After that you should want to add following code into top of the page.
session_start();
$_SESSION['username'] = $userNameValue;//set user name
echo $_SESSION['username'];
And then you should want to fetch data related to who logged user from your user table by using query.
Example
$query = "SELECT * YOURTABLENAME WHERE username='$_SESSION['username']'";
mysql_query($query, $connection) or die(mysql_error());
$data = mysql_fetch_array($query);
Then you can assign to $_SESSION['balance'] for value like this;
$_SESSION['balance'] = $data['balance'];
echo $_SESSION['balance'];
I've been coding for a short while now, and I am currently creating a website in PHP, html and with a mysql database. I got a table in my database for users, and my registration code works well : it registers the user, with the email adress, name, nickname and password he chose ; and an account with an already existing mail or nickname cannot be created.
My login code works... Almost.
<?php
session_start();
[...]
$query_users = "SELECT `name`, `password`, `id_user` FROM `users`";
$result_users = mysqli_query($link, $query_users);
while ($row = mysqli_fetch_assoc($result_users))
{
if (($row['name'] == $_POST['nickname']) && ($row['password'] == $_POST['password']))
{
$_SESSION['id'] = $row['id_user'];
$_SESSION['name'] = $row['name'];
header('Location: /Myblog/?action=login_success');
}
}
?>
You can't login if your username/password doesn't match with something in the database. But it seems that my $_SESSION aren't remembered by the server as soon as I change my page : check ?action=login_success
<?php
$user = $_SESSION['name'];
echo 'Welcome, $user.';
?>
And it only displays "Welcome, ." Does anyone know why it doesn't work ? Do I have to change all the way my code is built or is it just a little something that I forgot ?
You forgot to put session_start() in your second file. This function must be put in every file where you want to use sessions in
Also you forgot to double quote the welcome message, like that:
echo "Welcome, $user";
Single quotes don't parse variables.
I have come across an issue that has confused me a lot.
I am working on a login screen using PHP and MySQL. I manage to validate the username and password against an existing user in the database and after this, I initiate a session, and set the session variable username to the username provided in the login screen.
$username = html($_POST['username']);
$password = html($_POST['password']);
$result = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$result->bindValue(':username', $username);
$result->bindValue(':password', $password);
$result->execute();
foreach($result as $user)
{
$count = $count + 1;
}
if ($count == 1)
{
session_start();
$_SESSION['username'] = $username;
` //if I do an echo $_SESSION['username'] it displays the correct user
header('Location: .');
exit();`
}
However when it transfers me to the index.php page the $_SESSION['username'] variable has disappeared and I do not understand why. This is the code I use in index.php to check for the username:
<p>View all tasks</p>
<p>Add your own task</p>
<p>Welcome, <?php echo $_SESSION['username']; ?></p>
however I get the following error: Notice: Undefined variable: _SESSION in C:\xampp\htdocs\abcabcabc\index.php on line 16
All advice will be greatly appreciated guys
Add sesssion_start() to all of your scripts that use the session
You must use session_start(); at the beginning of index.php file.
The first 2 answers are correct. You must start session on each page. I usually just put it in a file that is "included" in every page already (like a header file) so I don't have to think about it.
i am making my own php game. So far i have made almost everything. Now to finish it, i need to get id from user who is logged in. I'm not so familiar with the functions and sessions. Please help.
This is what i made so far:
In my index page people login. then they are redirected to this.
So $_POST['username'] is where user type his user name in index.
<?php
$username = $_POST['username'];
include("Files/config.php");
$connect = #mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if($connect) {
if(mysql_select_db(DB_NAME)) {
$sql = mysql_query("SELECT * FROM users WHERE `username`='$username'") or die(mysql_error());
$gatherinfo = mysql_fetch_array($sql);
global $getid;
$getid = $gatherinfo['id'];
echo $getid;
function getuid() {
$_SESSION['getuid'] = $getid;
echo $getid;
}
}
}
else{ echo "Can not connect";}
?>
I searched other scripts for this, i found on one it says just $session->uid and it shows his id from mysql.
In mysql database i have table users with info about them
Id, username, password (password is hashed), email,...
Please help me if you can :D
At the beginning of index file (where your user logging in) start named session (be careful to avoid echo or print any values before session_start:
<?php //index.php
session_name('SAMPLESESSION');
session_start();
then when you will get the logged User ID, write this value to the session variable, like this:
.....
$_SESSION['uid'] = $getid;
.....
in the script you was redirected by your index file start session with the same name and get your user ID:
<?php //redirectedfromindex.php
session_name('SAMPLESESSION');
session_start();
echo $_SESSION['uid'];
....
If I right understand you, these that you need.