I can't seem to get my session variable to output. It works fine on the other one I'm using:
$_SESSION['status']['register']['username'] = $username;
header('Location: register-form.php');
When you are taken to register-form.php it should output the variable for you:
<?php
if(isset($_SESSION['status']['register']['username'])){
$username = $_SESSION['status']['register']['username'];
} else {
$username = '';
}
?>
Exact same code is used on email_address and works fine.
Here is the same code for the email
$_SESSION['status']['register']['email_address'] = $email_address;
And on the form page:
<?php
if(isset($_SESSION['status']['register']['email_address'])){
$ea_value = $_SESSION['status']['register']['email_address'];
} else {
$ea_value = '';
}
?>
You miss to initialize your session with
session_start();
at top of your register_form.php
Related
I'm currently having trouble with storing session data.
For some reason my sessions start and I can see it as a cookie but when I try to get my stored data from it it doesn't work.
Here is my code:
This is where I start the session and set the variables:
session_name("user");
session_start();
if ($groups != false) {
$_SESSION['groups'] = $groups->fetch_all(MYSQLI_ASSOC);
}
if ($dateformat != false) {
$_SESSION['dateformat'] = $dateformat;
}
$_SESSION['user_id'] = $row[0];
$_SESSION['user_firstname'] = $firstname;
$_SESSION['user_lastname'] = $lastname;
This is where I try to get my variables:
session_start();
if (isset($_SESSION['user_firstname'])) {
echo $_SESSION['user_firstname'];
}
Note: The page where I try to get the session data is my navbar/header it is only used with an php include statement.
It could be that $firstname is failing to be set. Try changing $_SESSION['user_firstname'] = $firstname; for example, to $_SESSION['user_firstname'] = "TEST"; and see if it stores that. If so, you're not giving $firstname a value.
I've a login page, where I'm setting the admin ID as a session variable
$_SESSION['adminUserId'] = $row['id'];
Now I've a header.php file which is called first on every page. To display the header.
And the first line of Header.php has
if (!isset($_SESSION['adminUserId'])) {
header("Location: ../index.php");
}
Now the strange part about this is, while I'm doing echo $_SESSION['adminUserId']), it displays the value of the variable. But when I'm checking the variable with isset, the result is false. I'm unable to understand this, as how this is happening.
Also, another strange thing, include header.php is the first line of code for every page, it works fine for all the pages apart from one, where it redirects the user to index.php
I've tried changing the variable name, setting the variable in a different way. But doesn't work for the specific page.
if (!isset($_SESSION['adminUserId'])) {
header("Location: ../index.php");
}
The expected result for a logged in user should be true but for a not logged in user it should be false, but it is showing vice versa
index.php (Here the session is getting set)
$qry = $DB_con->prepare("SELECT * FROM user WHERE username = '".$username."' AND password = '".$password."' AND role ='".$role."' AND country ='".$country."'");
$qry->execute();
$admin = $qry->fetchAll(PDO::FETCH_ASSOC);
// print_r($admin);
if($admin){
foreach($admin as $row)
{
if($username==$row['username'] && $password==$row['password'] && $role == 'Admin')
{
$country = $row['country'];
$_SESSION['Country'] = $country;
$DEO_id = $row['id'];
$_SESSION['adminUserId'] = $DEO_id;
$session_role = $row['role'];
$_SESSION['session_role'] = $session_role;
$usernameAdmin = $row['username'];
$_SESSION['city'] = $usernameAdmin;
$_SESSION['isAdminLoggedIn'] = "True";
header('location:admin/dashboard.php');
}
elseif($username==$row['username'] && $password==$row['password'] && $role == 'Data Entry User')
{
$DEO_id = $row['id'];
$_SESSION['dataEntry_ID'] = $DEO_id;
$country = $row['country'];
$_SESSION['Country'] = $country;
$session_role = $row['role'];
$_SESSION['session_role'] = $session_role;
header('location:data_operator/dashboard.php');
}
else
{
$error = "Invalid Username or Password.";
}
}
}
{
$error = "Invalid Username or Password.";
}
dashboard.php (Where the header.php is called and code works fine)
<?php include('header.php');
include('../include/dbcon.php');
?>
Now there's another link in the dashboard page
<a href="new-registration.php" class="btn btn-success btn-icon-split" style="width:100%">
<span class="text">New Registration</span>
</a>
When I see the new-registration.php page, this is how it looks like
<?php include('header.php');
include('../include/dbcon.php');
?>
And now the final header.php(where all this action is taking place)
<?php
include('../include/dbcon.php');
if(!isset($_SESSION['adminUserId']))
{
header("Location: ../index.php");
}
?>
I am trying to setup $_SESSION data and echo it out onto the page. I am defining the sessions in the header
<?php
$users = $mysqli->query('SELECT * FROM fn WHERE username = '.$_SESSION['username']);
if (is_object($users)) {
if ($users->num_rows) {
while ($row = $users->fetch_assoc()) {
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
$number = $row['number'];
$region = $row['region'];
$company = $row['company'];
$level = $row['level'];
}
}
}
$_SESSION['first_name'] = $first_name;
?>
I am then including the header.php file on my page and also using session_start();
I have no idea what im doing wrong.
I am managing to get the username and password to echo out... I assume its because it was added to the session upon login.
session_start() must come before you attempt to use $_SESSION variable, not after. Move your included file which contains session_start() to the top of that file for this to work.
You need to add session_start(). Put it after your PHP start tag
page1.php has this code
<?php
session_start();
$_SESSION['supp_id'] = 4;
?>
page2.php has this code
<?php
session_start();
include "db2.php";
$supp_id = $_SESSION['supp_id'];
var_dump($supp_id);
if(isset($_POST['accept']))
{
$cust_id = $_POST['cust_id'];
$cartype = $_POST['cartype'];
$q=mysqli_query($conn,"INSERT INTO `availablesupp` (`Cust_ID`,`Supp_ID`,`CarType`) VALUES ('$cust_id','$supp_id','$cartype')");
if($q)
echo "ok";
else
echo "error";
}
?>
$supp_id did not get passed into the query, and the database is updated with Supp_ID = 0;
However changing $supp_id = $_SESSION['supp_id'] to $supp_id = 4 works fine. var_dump produced identical results in both cases, int(4).
How do I solve this?
I get a field value using GET from a URL.
www.test.com?id=12345
I create a session using assigning the id to a variable.
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
if(isset($_SESSION['mainid'])) {
echo "Your session is running " . $_SESSION['mainid']; }
I then use echo session on other pages
if(isset($_SESSION['mainid'])) {
echo "Your session is running " . $_SESSION['mainid']; }
The problem is when i go back to the home page, it tries to look for the get value $id, and it is blank now as the URL doesn't contain the Feild value id=12345. How do i get around this ?
if(!isset($_SESSION)){
session_start();
}
if(!isset($_SESSION['mainid'])){
$id= $_GET['id'];
$_SESSION['mainid'] = $id;
} elseif(isset($_GET['id']) && isset($_SESSION['mainid'])) {
if($_GET['id'] != $_SESSION['mainid'] )
{
$_SESSION['mainid'] = $_GET['id'];
}
}
Well, you should rewrite your code for sure to check if the variable exists first and then start the session.
Something like this should do.
$id = isset($_GET['id']) ? $_GET['id'] : false;
if ($id) {
session_start();
$_SESSION['mainid'] = $id;
}
Just put your below code
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
inside isset
Your code should look something like below code:
if(isset($_GET['id'])){
$id= $_GET['id'];
session_start();
$_SESSION['mainid'] = $id;
}