Session variable not stored on other pages - php

i am using session variable to send details of user from login page to welcome page .
here is my code :
<?php
if(isset($_POST['login']))
{
$email=$_POST['email'];
$pass=md5($_POST['password']);
$a="SELECT * FROM users WHERE email='$email'AND password='$pass'";
$log=mysqli_query($con,$a);
$row=mysqli_fetch_array($log);
if(mysqli_num_rows($log)>0){
$_SESSION['firstname']=$row['first_name'];
$_SESSION['lastname']=$row['last_name'];
header("location:welcome.php");
exit;
}
else{
$er="login failed!";
}
}
on Welcome.php
<h2>WELCOME : <?php echo $_SESSION['firstname'];?></h2> <--- line 63-->
but i am getting this error :
Notice: Undefined index: firstname in
C:\xampp\htdocs\website\welcome.php on line 63
PS : kindly dont mark it as duplicate I tried many solutions but not helping. I used session_start(); on every page .

A session is started with the session_start() function.
in 1st page:
if(mysqli_num_rows($log)>0){
$row=mysqli_fetch_array($log);
session_start();
$_SESSION['firstname']=$row['first_name'];
$_SESSION['lastname']=$row['last_name'];
if(isset($_SESSION['firstname']))
header("location:welcome.php");
exit;
}
on page 2:you might have write this line at 1st line:
<?PHP
session_start();
?>
<h2>WELCOME : <?php if(isset($_SESSION['firstname'])) {echo $_SESSION['firstname'];}?></h2>

Edit: this answer is not correct and I will remove it when the comment-discussion has ended.
You fetch the first row of $log and then AFTER that check if the number of rows in $log is bigger than 0. Although you had one row initially, you fetched it! so at the time you check mysqli_num_rows($log) it will be 0, therefore never setting $_SESSION['firstname'] and $_SESSION['lastname'].
Try checking for number-of-entries before fetching the row like this:
$a="SELECT * FROM users WHERE email='$email'AND password='$pass'";
$log=mysqli_query($con,$a);
if(mysqli_num_rows($log)>0){
$row=mysqli_fetch_array($log);
$_SESSION['firstname']=$row['first_name'];
$_SESSION['lastname']=$row['last_name'];
header("location:welcome.php");
exit;
}

Related

i am try to create session but always session empty

I want to try to make a session but always session Empty.Use this code rate this product this link send an email open email and click link Active than code working session empty please help me...
<?php
session_start();
include("myhomeportal/setting/config.php");
$conform = $_GET['conform'];
$query = mysqli_query($conn, "SELECT * FROM item_users where com_code='$conform'");
$row = mysqli_fetch_array($query);
if ($row) {
// now update `com_code`
$sql = "UPDATE item_users SET com_code='active', user_type='user' WHERE com_code='$conform'";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
$inventory_id = $row['inventory_id'];
$active = $row['com_code'];
$_SESSION['sess_active'] = $active;
header("Location: category.php?inventory_id=$inventory_id");
} else {
// confirm code not found, show error
}
?>
Try to debug first.
Echo this $row['com_code']; then $_SESSION['sess_active'].
If they print something then go ahead.
There ir an error in your test page.
"sir other page is not working session just simple test code – Pankaj"
Instead you are only testing, you must start the session always with session_start() in every .php you want to use session.
Solving testing page.
"<h1>welcome <?php session_start(); echo $_SESSION['sess_active'];?> </h1> – Panka"
Note from w3schools:
The session_start() function must be the very first thing in your document. Before any HTML tags.
you are writting some html code before start the session, you should do that way:
<? php session_start(); ?>
<h1>welcome <?= $_SESSION['sess_active']; ?> </h1>
You can read more about this https://www.w3schools.com/php/php_sessions.asp

How to get a session uid to allow access to page

I am trying to get a certain uid number to access page if they are not in db then it redirects them to index page. Here is the code I put on top of the page. I keep getting an error this does not seem correct.
<?php
session_start();
if(!isset($_SESSION['uid'] = 12 )){
header('Location:index.php');
}
?>
You need to seperate this into two parts.
<?php
session_start();
if(!isset($_SESSION['uid']) || $_SESSION['uid'] != 12){
header('Location:index.php');
exit;
}
?>

<?php echo $_SESSION doesnt display on site

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'];

PhP and mySQL testing

I have been trying to test a login php but after i put the username and password i get this error(Notice: Undefined index: username in /web/stud/u1177827/store_admin/admin_login.php on line 12 Notice: Undefined index: password in /web/stud/u1177827/store_admin/admin_login.php on line 13 Faild connection.That information is incorrect, try again Click Here)
here is the code:
<?php
session_start();
if(isset($_SESSION["staff"])){
header("location:index.php");
exit();
}
?>
<?php
// Prase the log in form if the user has filled it out and pressed "Log in"
if(isset($_POST["username"])&& isset($_POST["password"])){
$username=preg_replace('#[^A-Za-z0-0]#i',"",$_SESSION["username"]);//filter everything but numbers and letters
$password=preg_replace('#[^A_Za-z0-9]#i',"",$_SESSION["password"]);//filter everything but numbers and letters
//Connect to the MySQL database
include "../storescripts/connectToMySQL.php";
$sql=mysql_query("SELECT*FROM B4UStaff WHERE fname='$username' AND lname='$password'LIMT 1");//query the person
//..... MAKE SURE PERSON EXISTS IN DATABASE....
$existCount=mysql_num_rows($sql);//count the row nums
if($existCount==1){//evaluate the count
while($row=mysql_fetch_array($sql)){
$staffNo=$row["staffNo"];
}
$_SESSION["staffNo"]=$staffNo;
$_SESSION["username"]=$username;
$_SESSION["password"]=$password;
header("location:index.php");
exit();
} else{
echo 'That information is incorrect, try again Click Here';
exit();
}
}
?>
Does any one know how to fix it?
Correct this two lines instead of SESSION you have to use POST like this
$username=preg_replace('#[^A-Za-z0-0]#i',"",$_POST["username"]);//filter everything but numbers and letters
$password=preg_replace('#[^A_Za-z0-9]#i',"",$_POST["password"]);//filter everything but numbers and letters
Simply change $_SESSION with $_POST:
$_SESSION["username"]
$_SESSION["password"]
to
$_POST["username"]
$_POST["password"]
In your code, you need to get these two POST variables and set it as SESSION variables with staffNo which is correct.

Var.$logged defined in global.php, not picked up by index.php

I was attempting to follow along with the tutorial at the YouTube channel Helping Develop
when I came across a few issues. The first being his logout code in tutorial #5 doesn't work anymore due to the changed
if(session_is_registered()
I think I have replaced that properly with
if( isset($_SESSION[$username])){
But now I am getting an error that says
"Notice: Undefined variable: logged in C:\xampp\htdocs\membership\index.php on line 2"
When I check line 2 of index it shows that i am including global.php first
<?php include_once('scripts/global.php');
if($logged==1){
header("Location:home.php");
exit();
}
?>
So then I check global.php which has $logged=1 so it should be defined...unless I am missing something. I am really trying to learn more here, so any help would be appreciated in explaining what is wrong, and why.... Thank you.
<?php
session_start();
include_once('scripts/connect.php');
//checking if the sessions are set
if(isset($_SESSION['username'])){
$session_username=$_SESSION['username'];
$session_pass=$_SESSION['pass'];
$session_id=$_SESSION['id'];
//checking the member data
$query=mysql_query("SELECT * FROM members WHERE
id='id' AND password='pass' LIMIT 1")or die("Could not check member");
$count_count=mysql_num_rows($query);
if(count_count>0){
//logged in stuff here
$logged=1;
}else{
header('Location:logout.php');
exit();
}
}elseif(isset($_COOKIE['id_cookie'])){
$session_id=$_COOKIE['id_cookie'];
$session_pass=$_COOKIE['pass_cookie'];
//checking the member data
$query=mysql_query("SELECT * FROM members WHERE
id='$session_id' AND password='$session_pass' LIMIT 1")or die("Could not check member");
$count_count=mysql_num_rows($query);
if(count_count>0){
while($row=mysql_fetch_array($query)){
$session_username=$row['username'];
}
//create sessions
$_SESSION['username']=$session_username;
$_SESSION['id']=$session_id;
$_SESSION['pass']=$session_pass;
//logged in stuff here
$logged=1;
}else{
header('Location:logout.php');
exit();
}
}
?>
$logged is set inside a conditional, so trace back through to understand how you might not hit a random redirect (reminds me of the crazy "goto" days which I through we progressed past) and end up with $logged not being set. Or initialize $logged at the beginning of the included script.

Categories