Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When user is logged in have to show logout otherwise it will show log in bar.... I am trying the bellow code :
<?php
if(session_start())
{
echo "<a href='home.php'>Logout</a>";
}
else echo " <a href='index1.php'>Login/Register</a>";
?>
// But this is not working. Kindly help
You are calling session_start() that will start the session. What you need to do is create the login script and then start the session. I would perhaps set a $_SESSION variable at this point like $_SESSION['logged_in'] = true. Then in the navigation header check for
if($_SESSION['logged_in'] === true){
// do something
}
That would perhaps be a better way to handle this.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I started session with session_start() and set it like $_SESSION['auth'] = 1; and the url is something like localhost:8000/home after the login.
Now the problem is that why I am able to see my home page in different browser, it shouldn't be, just like google and stackoverflow pages after login. It should redirect me to the login page. How is that happening. How to destroy session on new browser?
if(isset($_SESSION['auth']))
{
session_destroy();
redirect('/login');
}
Try using session_unset() like this:
if(isset($_SESSION['auth']))
{
session_unset();
redirect('/login');
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm using this code to redirect but it shows error
<?php
header(Location: . site_url("./admin/dashboard"));
die();
I know its a small problem. Still if anyone can help I will be grateful
You should add quotes to your string.
<?php
header('Location:' . site_url("./admin/dashboard"));
die();
Use something like this, it's a good idea to put the full address to redirect but can do this with the relative address.
Note: you must put Location: in quite.
<?php
header("Location: ". site_url("./admin/dashboard")); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am working on a e-commerce website. I want a help regarding displaying login & logout but when a user is logged in, user should only get logout option & when logged out user should get login option.
Please help me regarding this concept.
you can create SESSION variable (with email or any unique attribute) and set it when user LogsIn.
eg: session_start();
$_SESSION['email'] = useremail;
and check in every page if session varible is set or not.
session_start();
if(!isset($_SESSION)){
//then show login button
}
else{
//show logout button
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using the login form of this website http://www.html-form-guide.com/php-form/php-login-form.html, And i would like to display:
If you are connected, display Welcome ["name"]
Else if you are not, display Sign Up, Login.
I tried different ways, but it doesn't work. Could you help me please.
Thank you a lot, regards.
In addition with Mr.Alien answer, Use SESSION function to do this.
Put a user_id in session, show welcome message if session been set, show Sign up | Login if session not been set.
$_SESSION['user_id'];
if(isset($_SESSION['user'])) {
echo 'Welcome <username>';
}else{
echo 'Sign Up';
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I had a problem facing that when we are in a page after login to the site then after some time we do loggedout from the page
Now my question is that after logged out if anyone copies the url and opened in the another browser it should be direct them to the login page
The code should contain the function
and that function should call in another php file like call function()
write below code on logout.php
session_start();
//or #session_start() if session already started before this line
session_destroy();
session_unset();
session_write_close();
session_regenerate_id(true);
$_SESSION = array();
header("Location:index.php");
exit();
more examples can be seen on session manual