i am try to create session but always session empty - php

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

Related

Remove variable from URL

This is for a simple login system, any time a user receives an error I redirect them to a another page (using header(Location:...)) and put the error ID in the URL so that I know which error to display.
login.php
<?php
if(isset($_GET[ 'status'])){
if($_GET[ 'status']=='error' ){
?>
<p style='color:red;'>Incorrect Username/Password combination!</p>
<?php
} }
?>
Checklog.php
<?php
include('../dbconnect.php');
$username =$_POST['form-username'];
$pwd =$_POST['form-password'];
$result = mysql_query("SELECT * FROM member WHERE email='$username' and password='$pwd'") or die ('Query failed:'.mysql_error());
//$admin=mysql_query("SELECT * FROM admin where user='$username' and code='$pwd'") or die ('Query failed:'.mysql_error());
if (mysql_num_rows($result)>0){
session_start();
$_SESSION['user']=$username;
//echo "hello $username";
header("Location:../homepage.html");
}
/*else if (mysql_num_rows($admin)>0){
session_start();
$_SESSION['admin']=007;
header("Location:add1.php");
}*/
else
{
header("Location:login.php?status=error");
}
mysql_free_result($result);
mysql_close($con);
?>
On refresh the error message is still here, is there a solution how i can redirect the user to the original login.php without the error message if he refresh
You can use javascript window.history.pushState to alter the URL after the document has fully loaded. This will change the URL in the address bar and affects the address they are taken to on refresh.
Alternatively you can store messages in the $_SESSION and clear the message after it has been displayed.
I agree with henrys answer. Personally id use $_SESSIONS but as as your question was to remove url parameters the only way would be js.
For example
history.pushState(null,null, window.location.href.split('?')[0]);
This is untested but should do what you want in a single line of code.

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

Updating user information

I know I can't use two session start codes in a same php page but for the sake of updating user account, I need the below code and I need to use session_start twice. One, to check if the user is not logged in, then redirect them and banned them from seeing the update info page and also the other session start has to be there so that my session variables could be set automatically in the update info page if the user is logged in.
anyways, I am getting this error can you guys please show me a work around way? if there's any?
thanks.
Notice: A session had already been started - ignoring session_start() in ....
<?php session_start();
if(isset($_SESSION['userid'])) {
} else {
header('Location: login.php');
}
?>
<?php
$user = $_SESSION['userid'];
$myquery = "SELECT * FROM our_users WHERE `userid`='$user'";
$result = mysqli_query($conn, $thequery);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
session_start(); /* Basically this right here gets ignored. */
$_SESSION["user_first_name"] = $row['fn'];
$_SESSION["user_last_name"] = $row['ln'];
$_SESSION["user_email"] = $row['em'];
$_SESSION["user_password"] = $row['pw'];
?>

PHP - Managing Session - Doesnt Logout

I have created a user authentication system with necessary DB tables and php.
THe first time before I login (Before any SESSION is created) the redirect on every page works perfect (ie Redirects to the login page if not logged in).
But once I login with a user and then logout the same doesnt work. I think it might be a problem with not ending the SESSION (Sorry if am wrong)
Here are some pieces of the code in each Page
Login PHP
<?php
session_start();
$message="";
if(count($_POST)>0)
{
include('config.php');
echo $_POST['username'];
$result = mysql_query("SELECT * FROM members WHERE username='" . $_POST["username"] . "' and password = '". $_POST["password"]."'");
$row = mysql_fetch_array($result);
if(is_array($row))
{
$_SESSION["id"] = $row[ID];
$_SESSION["username"] = $row[username];
$_SESSION["password"] = $row[password];
$_SESSION["mname"] = $row[mname];
$_SESSION["fname"] = $row[fname];
date_default_timezone_set("Asia/Calcutta");
$lastlog=date("d/m/Y");
$logtime=date("h:i a");
$query = "UPDATE `members` SET `lastlogin`='$lastlog',`logintime`='$logtime' WHERE `ID`='$row[ID]'";
mysql_query($query);
$_SESSION['logged'] = TRUE;
}
else
{
echo "<SCRIPT>
alert('Wrong Username/Password or Awaiting Approval');
</SCRIPT>";
header("Location:login_failed.html");
}
}
if(isset($_SESSION["id"])) {
header("Location:member/myprofile.php");
}
?>
PHP code on every page
<?php
session_start();
include('config.php');
if(!$_SESSION['logged'])
{
header("Location: ../login.html");
exit;
} ?>
And Finally Logout
<?php
session_start();
unset($_SESSION["id"]);
unset($_SESSION["username"]);
unset($_SESSION["password"]);
unset($_SESSION["mname"]);
unset($_SESSION["fname"]);
header("Location:../login.html");
?>
Is there any problem with my Code. Am i missing something? I couldn't get it right. Pls Help
Thanks guys got it solved..
Now can you tell me How I can redirect login.php to user home page(myprofile.php) in case the User is logged in (Session exists) - Like facebook,gmail etc
Instead of calling unset() on each session var, you can simply use session_destroy(), which will destroy all of the current session data.
session_start();
session_destroy();
header("Location:../login.html");
For complete destructive power, you might also want to kill the session cookie:
setcookie(session_name(), '', 1);
See this question for a more complete example of session logout.
You need to unset $_SESSION['logged']
Also you should reference keys in the $row variable with strings. Eg $row['username'];.
Turning on E_NOTICE level warnings with error_reporting will help you with this.
If you haven't already, reset the session login
unset($_SESSION['logged']);
Or just change it to false
$_SESSION['logged'] = false;
When you are directly hitting a page in address bar for the first time then its a new request which goes to the server and server checks for existing session as written in your code. But its not same when you are pressing back button after logout. In this case there is no request is going to the server instead the request is fetched from browser cache. If you want to disable this situation then you have to tell browser explicitly to not to store your page in cache memory. For more detail please go through this link

trying to set session variable

if(isset($_SESSION['admin'])) {
echo "<li><b>Admin</b></li>";
}
<?php
session_name('MYSESSION');
session_set_cookie_params(0, '/~cgreenheld/');
session_start();
$conn = blah blah
$query2 = 'Select Type from User WHERE Username = "'.$_SESSION['user'].'" AND Type =\'Admin\'';
$result2 = $conn->query($query2);
if($result2->num_rows==1) {
$_SESSION['admin'] = $result2;
}
?>
Hi, I'm trying to set this session variable but it doesn't seem to be setting, and i'm wondering if anyone can help. If session['admin'] isset it should echo the admin button.
But i'm not quite sure why? (I do have session start and everything on everypage, it's not a problem with that or any of the "You don't have php tags" I have checked the mysql query, and it does return something from my table. Any ideas please?
Your session_start(); should be at the top of the page before anything to do with the session variables.
From the docs:
When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.
Edit from comments:
<?php
session_name('MYSESSION');
session_set_cookie_params(0, '/~cgreenheld/');
session_start();
// Moved to start after answer was accepted for better readability
// You had the <?php after this if statement? Was that by mistake?
if(isset($_SESSION['admin']))
{
echo "<li><b>Admin</b></li>";
}
// If you have already started the session in a file above, why do it again here?
$conn = blah blah;
$query2 = 'Select Type from User WHERE Username = "'.$_SESSION['user'].'" AND Type =\'Admin\'';
// Could you echo out the above statement for me, just to
// make sure there aren't any problems with your sessions at this point?
$result2 = $conn->query($query2);
if($result2->num_rows==1)
{
$_SESSION['admin'] = $result2;
// It seems you are trying to assign the database connection object to it here.
// perhaps try simply doing this:
$_SESSION['admin'] = true;
}
?>
Edit 2 from further comments:
You have to actually fetch the fetch the data like this - snipped from this tutorial which might help you out some more:
$query = "SELECT name, subject, message FROM contact";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['name']} <br>" .
"Subject : {$row['subject']} <br>" .
"Message : {$row['message']} <br><br>";
}
But having said that, while we are talking about it, you would be better off moving away from the old mysql_* functions and move to PDO which is much better.
Move session_start(); to the top of the page. You are trying to retrieve sessions, where it's not loaded.
EDIT: Try echoing $_SESSION['admin'], if it even contains something. Also try debugging your if($result2->num_rows==1) code by adding echo('its working'); or die('its working'); inside it, to check if $result2 contains exactly 1 row, since currently it seems $result2 contains either more than 1 row or no rows at all.

Categories