Multiple User Section [more than one page] - php

I'm starting a session and redirect after successful login to a home.php
now my question is how can i let access the users more pages. i thought about:
<?php
session_start();
if(isset($_SESSION['user_session'])!="")
{
header("Location: home.php");
header("Location: home2.php")
}
?>
this page should only useable to the users. First the user should be redirected to home.php and then the user should get access to another page like home2.php but a non-user shouldn't get access to this page.
When the user is at home.php i thought about a simple <a></a> redirect with html to home2.php

Create a session and make an <a></a> Tag that only registered useres can see.
You have to session_start() in every site and Theo redirect if the User is already logged in

Related

Session only registering when set directly from the login page

<?php
session_start();
if(isset($_SESSION['login_user'])) {
require_once('logged.html');
} else {
require_once('notlogged.html');
}
?>
This code works fine when I login directly from the login page, but when I start from the homepage, then a href to the login page, login, it still shows the notlogged.html.
But when I start from the login page, I can login and get to logged.html, go to the signout page and destroy my session, which lands me to the notlogged.html, then login again to the logged.html.
So basically, it works perfectly when I enter from login.php, but not when I enter from index.php. Any idea why this might be?
question isn't clear, 1st of all check whether session created while login from any page, its simple after creating session variable, make alert using JS
alert("$_SESSION['login_user']");

php allow access to pages to only logged in users

I have 3 pages have html code similar to just example
<!DOCTYPE html>
<html>
<body>
<div>
html code
</div>
<div>
html code
</div>
<div>
html code
</div>
</body>
</html>
and have 2 php files login.php and logout.php , is it possible to strict access to the 3 pages only to login users
First, Put in header page this session_start();, Ofcourse the header page is included||required in every php page you have.
Second, When the user Login using your Login page, Put the sessions if his data are valid
<?php
if($user && password_verify($password, $user['password'])){
$_SESSION['id'] = $user['id'];
}
?>
In this one we used his id inside session, Now all you have to do is checking if this session is active, If it is not active, You redirect the visitor using header() to the index||404 page like this
<?php
if(!isset($_SESSION['id'])){
die(header("location: 404.php"));
}
?>
and remove the ! for signup & login pages, Since you don't want a logged in user to access the login or register page again.
Third, For logout page, Just put
<?php
session_start();
session_unset();
session_destroy();
header("location: index.php");
exit();
?>
inside it
This answer assumes that you have 3 php files and not html. You need to save those files as .php if you want to manage this using PHP.
Yes you can manage that using a variable or session.
You can redirect the user if they are not logged in. Or, you can show the part of the text and link only if they are logged in.
<?php
if($logged_in) {
?>
Only for logged in users
<?php
}
?>
You need to use session to do it in PHP, it will be like this.
$_SESSION(id)
You can see some tutorial in google.
Refer the below link http://www.makeitsimple.co.in/PHP_loginexmp.php

iN wordpress...I want dont want to allow user to see any page without login

iN wordpress.I want dont want to allow user to see any page without login. at the moment that can see home page.but I want to redirect them to login page without login.they cannt access any page of the website without login.any one has an idea how to do this in wordpress??
header.php is included in each page,
you can check with this get_current_user_id(); this function returns 0 if not any user in logged in.
in header.php
<?php
if( !get_current_user_id() && !preg_match('/login/', $_SERVER['REQUEST_URI'])){ // checks if the user is not logged in and the page is not login page,
// redirect to some other page,
header("Location: ". site_url().'/wp-login/' ); // redirecting to login page
exit()
}
?>
WordPress has a function to check for whether a user is logged in called is_user_logged_in().
So the logic to place at the top of the header.php would be:
<?php
if ( !is_user_logged_in() ) {
header("Location: http://www.LoginPageUrl.com/");
}
?>

Homepage not immediately recognizing session PHP

I have a site that displays two different versions of a navigation section depending on if a user is logged in or not.
<?php
if(isset($_SESSION['myusername'])){
echo 'Log Out';
}else{
echo 'Sign Up';
}
?>
The problem happens when a user is logged in and then closes the browser without logging out (and assuming they don't clear cache/cookies on browser exit).
When they open their browser later and come back to the site, the navigation displays as if they're not logged in. If they then click a link elsewhere on the site, i.e. My Account, the navigation then changes to show that they are logged in.
Any ideas what could be causing this? I'd like the navigation to show that they're logged in immediately upon coming back to the site.
First thing, check session_start() appears on your pages before any html, even the !DOCTYPE rule.
Now, on your index page add this:
<?php
session_start();
if(isset($_SESSION['username'])){
header("location: home.php"); // or whatever page you want your users to be redirected to...
}else {
?>
// here your html page should start
<html><head></head><body>
// all the DOM elements on your page
</body></html>
<?php
} // closing end of the else block started above
?>
Must be as below.
ob_start();
session_start();
//code to check session and other
ob_start() is for omitting header already sent error.

Display Admin Link On Every Page

My question is about how do I place a link on the side of every page that leads to the admin page once someone has logged on to my application successfully? I have a sample site I just built, and I would like a link to the admin page available in the navigation column to the right of the page which is displayed site-wide. But if the person is not logged in, they don't see the link, but will continue to see the usual links.
My background is totally different from web development, so forgive my stupid question.
I'm using PHP and MySQL for the application.
Without seeing how you display your menu or what key is used for the session, Ill assume some things:
<?php
session_start();
// do your login stuff and set session as logged in
$_SESSION['logged_in'] = true;
?>
Then in your menu or how ever you display it:
<?php
//navigation column
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true){echo 'Admin';}
//navigation column continue with rest of links
?>
Or the ternary operator assign link to a variable
<?php
$adminLink = (isset($_SESSION['logged_in']) && $_SESSION['logged_in']===true)?'Admin':'';
echo $adminLink;
?>
You should use a session variable to track the user's session and see if they are logged in.
if(isset($_SESSION['id'])) echo 'Admin Area';

Categories