Iam having issues echoing the session username and have no idea were to start any more cause every thing i have tried keeps saying string to array conversion but idk how to get this to correctly work. here is the code that would check for the loged in saying you logged in would you like to log out.
if(isset($_SESSION['username'])&& !empty($_SESSION['username'])){
echo "<P id='loged'>your loged in would you like to log out</p>";
echo'log out';
}else{
include 'logform.php';
}
You probably want to call session_start() before the $_SESSION variable will be available.
You just want to echo the username?
echo "Hello " . $_SESSION['username];
I suppose the following answers your question...
echo $_SESSION['username'];
...but you will also want to use session_start() first.
Also, no need to use isset() and !empty(), as empty() checks if it is set first. All you really need is:
if(!empty($_SESSION['username']))
You forgot to mention session_start() in the begining.
session_start(); //add this in the begining of the file.
echo $_SESSION['username']; //this will display username
Try this:
call the function session_start() at beginning of the page and then check the $_SESSION array using
echo '<pre>';
print_r($_SESSION);
after that you see all the indexes set in session variable.
Thanks
Related
On my site, the login.php page (if successful login) will redirect to index.php and will start a session and 2 SESSION variables.
One of the variables started is a success message:
$_SESSION["message"] = "Login successful!";
the second is the user session variable:
$_SESSION["authenticatedUserEmail"] = $email;
the problem is that if I check the variables individually and then try and use them on the index.php page, only the first one that is checked will work.
This following snippet will show the $form_message but it will not show the $_SESSION["authenticatedUserEmail"]:
session_start();
if(isset($_SESSION["message"])) {
$form_message = $_SESSION["message"];
session_unset($_SESSION["message"]);
echo $form_message;
} else {
$form_message = "";
}
if (isset($_SESSION["authenticatedUserEmail"])) {
echo $_SESSION["authenticatedUserEmail"];
}
It does work when I only use one if(isset($_SESSION statement but I don't want to always include both inside the same statement.
I've done an error check:
ini_set('display_errors',1);
error_reporting(E_ALL);
but no errors appear.
Can anyone please suggest why this may not be working or if I am missing something?
Thanks in advance.
session_unset function free all session variable. this is why when you are using session_unset, next session variable is not founded. read the manual please.
http://php.net/manual/en/function.session-unset.php
to achieve what you want you can use unset function
unset($_SESSION["message"]);
hope this helps
Your call to session_unset is the problem, you should be simply using unset.
session_unset unsets the whole $_SESSION array.
I am trying to verify that a user has logged in before showing them the page, using the method below, while the if/else method works when wrapped around plain html, it is failing when there is php involved. I am a novice by the way. What happens is the page simply loads as if the two tags below weren't there...which would be fine had I previously logged in, but I hadn't.
<?php
session_start();
if(isset($_SESSION['user'])) {
?>
HTML/PHP Page goes here.
<?php
} else {
header("Location: cms/admin/loginreadmode.php");
}
?>
Thanks in advance,
You can debug just below your session_start(); by printing your session:
echo '<pre>';
print_r($_SESSION);
die();
If $_SESSION['user'] isn't showing up in your array it isn't be set.
You can do this like this:
session_start();
$_SESSION['user'] = true;
Are you sure that you have add session support in every page?
if (!isset($_SESSION)) {
session_start();
}
This code should be working, so mistake is probably somwhere else I suggest checking if you set $_session["user] after login.
You should also replace your not-working code part with simple
echo "hello";
to chek it.
1) That is not a great method of checking whether a user is logged in, purely checking whether a user sessions exists can end up causing a lot of problems. Storing the ID in the sessions and then checking whether the ID is valid may be a better way,
2) When I copy the code above into a test document it goes straight to the redirect page in the else statement. This is down to the user session not being set, as soon as I set the user session before the code is executed it works fine. I see 'HTML/PHP Page goes here.'.
Setting the user session:
$_SESSION['user'] = 'TestUser';
You can change the code at the top of the page to be
<?php
session_start();
if(!isset($_SESSION['user'])) {
header("Location: cms/admin/loginreadmode.php");
die();
}
?>
I have created a php session and have assigned data like so
session_start();
$_SESSION['desd']=$imgD;
When i try to retrieve it on other page i get nothing.
this is what i have done on second page,
session_start();
$_imgname = $_SESSION['desd'];
How to do that?
The code is valid, assuming that the variable $imgD is defined (var_dump is one way of checking this, but print or echo may also work).
Check to ensure that cookies are enable in your browser.
Also, be sure that session_start() comes near the top of your script, it should be the first thing sent to the client each time.
To test a session, create "index.php" with the following code:
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;
echo "views = " . $_SESSION['views'];
?>
Reload the page several times and you should see an incrementing number.
An example of passing session variables between two pages is as follows:
PageOne.php
<?php
session_start();
// makes an array
$colors=array('red', 'yellow', 'blue');
// adds it to our session
$_SESSION['color']=$colors;
$_SESSION['size']='small';
$_SESSION['shape']='round';
print "Done";
?>
PageTwo.php
<?php
session_start();
print_r ($_SESSION);
echo "<p>";
//echo a single entry from the array
echo $_SESSION['color'][2];
?>
For simplicity put PageOne.php and PageTwo.php in the same directory. Call PageOne.php and then PageTwo.php.
Try also tutorials here, here, and here.
If you call session_start(), the server gets the session ID from cookies.
Maybe something is wrong with the cookies because the code is valid.
I have everything working on my login except for the admin user.
When I try to navigate to my admin.php I get "Must be logged in" even if I am already logged in as an admin.
When I click the admin link, I first go to checkAdmin.php which is the code below.
<?php
if($_SESSION['userEmail'] != 'admin#hotmail.com') {
echo "<center><font face='Verdana' size='2' color=red>Must be logged in</font> <br><a href=index.php>Login Here</a></center>";
exit;
}
?>
Am I doing something wrong on this?
Thanks.
I have session_start(); at the top of my admin.php page.
I used print_r($_SESSION); and got this line, so it has the right value.
Array ( [userEmail] => admin#hotmail.com )
Also, if I add session_start(); to the checkAdmin.php then the screen just sits at checkAdmin, when I click to go to the admin page.
The admin.php has this at the top
session_start();
require "checkAdmin.php";
include("db_info.php");
Make sure you have called session_start() before checking session variables.
you probably did not set $_SESSION['userEmail'] while the login-process
also session_start() has to be called.
I'd check what really is in your session with print_r($_SESSION);
Did you try to add session_start() ? You have to do it before looking for session variables.
Try with it :
<?php
session_start();
if($_SESSION['userEmail'] != 'admin#hotmail.com') {
echo "<center><font face='Verdana' size='2' color=red>Must be logged in</font> <br><a href=index.php>Login Here</a></center>";
exit;
}
?>
use var_dump() instead of print_r. pay carefull attention to the reported string length, because some characters arent visible, but will obviously cause a string comparison to fail.
you likely have a stray whitespace character
Hello i am having problems holding sessions from page to page, code worked on my previous servers running php5 but not on my recent server, i am wondering whether its a bug?
<?php
session_start();
$_SESSION['session'] = $_POST['session'];
header("location: www.mysite.com/page1.php");
?>
<?php
session_start();
echo "Good morning" . $_SESSION['session']; //returns empty session always.
?>
ANy ideas? session is held on first page but not on the second.
In case you missed it, make sure you do a session_start() at every page you're using the $_SESSION variable.
You should check your php.ini file and see what's going on.
Make sure session.use_cookies = 1 and session.save_handler = files.
Use this test page to see whether it's a general PHP problem or just your code.
<?php
session_start();
if(isset($_SESSION)){
echo "Session variable exists<br/>";
if(!isset($_SESSION['test'])){
$_SESSION['test'] = "Success!";
echo "Variable has been set, refresh the page and see if stored it properly.";
}else{
echo $_SESSION['test'];
}
}else{
echo "No session variable has been created.";
}
?>
If that worked, then it's got to do with your code.
If you're setting your session variable to $_POST['session'] am I to assume you submitted a form with an input with the name session?
This setup should work.
index.php
<form action='page0.php' method='POST'>
<input type='hidden' name='session' value='SPAAAAACE' />
<input type='submit' />
</form>
Page0.php
<?php
session_start();
$_SESSION['session'] = $_POST['session'];
header("location: www.mysite.com/page1.php");
?>
Page1.php
<?php
session_start();
echo "Good morning" . $_SESSION['session'];
?>
For completeness and debugging purposes
In case you are using cookie-less sessions, you have to manually add the SID (session id) to the header redirect like this
header("location: www.mysite.com/page.php?".htmlspecialchars(SID));
If the problem still persists, it could be a permission issue.
Maybe you're not allowed to read the session file stored on the server?
Update: OP commented that it was a permission issue and the problem is now resolved
Turn on error reporting temperately with:
error_reporting(E_ALL) This may spit out an error related to your problem. Most likely an undefined index session notice.
You should always have a check in place on Super Globals.
<?php
session_start();
$_SESSION['session'] = (isset($_POST['session']))?$_POST['session']:null;
header("Location: www.mysite.com/page1.php");
die;
?>
Your code seems correct though I'm pretty sure $_POST['session'] is empty.
You should try this :
<?php
session_start();
$_SESSION['session'] = 'John Doe';
header("location: www.mysite.com/page1.php");
?>
<?php
session_start();
echo "Good morning" . $_SESSION['session']; //returns empty session always.
?>
To see if this works or not. I guess it will.
IF not, take a look at your cookies, maybe they are disabled.
Then, if it works, I probably because $_POST['session'] is null or empty, are you sure you posted something like <input type="text" name="session" /> ?
You need to pass the session id with the redirect.
Also make sure you use session_start() at the top of EVERY page that needs a session
First try using
<?php session_start();
instead of
<?php
session_start();
If the problem still exists, then open your script in Netbeans editor and see whether any unexpected characters found at very beginning of the the script.
In addition, please make sure that $_POST['session'] has a value to assign in $_SESSION['session'].
You will have to call
session_start();
on the first line of every page you want to retain the session in