I've been looking for an answer to make all my other .html pages in the root directory(or outside of) to be checked against if a session has taken place or not. If not direct visitor back to index.php (where login is, set in public_html). Does anyone have a work around for this? If so where would I put the "if no session redirect"? So if someone just goes directly to www.site.com/here.html the php runs and see's if the session had been instated, if not redirects to www.site.com/login.php.
Asked
Active
Viewed 1,475 times
-2
-
Google "mod rewrite" and make sure you instructed Apache to treat `.html` files as PHP; if that's what the question's about. You could also use conditional statements, checking against if the session is set or not. – Funk Forty Niner Aug 04 '15 at 16:35
-
just saw this comment and haven't researched enough to know how to implement Apache to use it to treat .html files as .php files. But it is seeming that I will need to do this as some point to make my dreams come true. – handpaintedstudio Aug 05 '15 at 22:21
2 Answers
0
Use this:
<?php
if(!session_id()){
header('Location: /index.php');
exit;
}
?>
Just place it at the top of your html file
Or to be on the safe side, check if an index in the session is set like:
if(!isset($_SESSION['user'])){
// redirect code
}
For this to work, on user login, you need to set
$_SESSION['user'] = 'something'; // a real value here!
Alex Tartan
- 6,736
- 10
- 34
- 45
-
index.php is where my user login is set. would it be safe utilize: $_SESSION['user'] = 'somehting'; // something would be equal to the user name? – handpaintedstudio Aug 04 '15 at 17:13
-
Does this work at a top of a .html page or only .php I've been told many time I can do it at the top of any html page but that never works. – handpaintedstudio Aug 04 '15 at 17:22
-
To run php code inside html files, follow this http://stackoverflow.com/questions/4687208/using-htaccess-to-make-all-html-pages-to-run-as-php-files – Alex Tartan Aug 04 '15 at 18:01
-
-
yes, but I am new but not that new. just looking for help on learning more. – handpaintedstudio Aug 06 '15 at 21:55
0
Ok Alex, this is what I came up with but haven't ran it yet. Just looking for if I'm even coming close. Surely I'm not, this is my first go at adding this type of functionality to a site.
<?php
function menuOptions($menuOptions)
{
$menu1 = array(
'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
);
$menu2 = array(
'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
'Marketing-Projects' =>('text'=>'Marketing-Projects', 'Marketing-Projects.html'=>'?p=Marketing-Projects'),
'MarketingSchedules' =>('text'=>'Marketing Department Schedules', 'MarketingSchedules.html'),
'MarketingDepartmentSchedules' =>('text'=>"Marketing Department Schedules, MarketingDepartmentSchedules.html"),
'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
);
$menu3 = array(
'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
'Marketing-Projects' =>('text'=>'Marketing-Projects', 'Marketing-Projects.html'=>'?p=Marketing-Projects'),
'MarketingSchedules' =>('text'=>'Marketing Department Schedules', 'MarketingSchedules.html'),
'MarketingDepartmentSchedules' =>('text'=>"Marketing Department Schedules, MarketingDepartmentSchedules.html"),
'MarketingExpenseReports' =>('text'=>"Marketing Expense Reports", 'MarketingExpenseReports.php'),
'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
);
$menuAdmin = array(
'Marketing Dept.' =>('text'=>'Marketing Dept.', 'Welcome.html'=>'?p=home'),
'Printing' =>('text'=>'Printing', 'Printing.html'=>'?p=Printing'),
'Email Marketing' =>('text'=>'Email Marketing', 'Emailing.html'=>'?p=Emailing'),
'Website Development' =>('text'=>'Website Development', 'Website.html'=>'?p=Website'),
'Marketing-Projects' =>('text'=>'Marketing-Projects', 'Marketing-Projects.html'=>'?p=Marketing-Projects'),
'MarketingSchedules' =>('text'=>'Marketing Department Schedules', 'MarketingSchedules.html'),
'MarketingDepartmentSchedules' =>('text'=>"Marketing Department Schedules, MarketingDepartmentSchedules.html"),
'MarketingExpenseReports' =>('text'=>"Marketing Expense Reports", 'MarketingExpenseReports.php'),
'Help' =>('text'=>'Help', 'Help.html'=>'?p=Help'),
'Logout' =>('text'=>'Logout', 'logout.php'=>'?p=Logout'),
);
}
function aprovedusers($aprovedusers)
{
// setting aproved users list 1 for sales department
$aprovedusers1=array("user1");
// setting aproved users list 2
$aprovedusers2=array("user1","user2");
// setting aproved users list 3 for marketing department
$aprovedusers3=array("user1","user2","user3");
// setting aproved users list admin for admin
$aprovedusersAdmin=array("Admin1");
$msg1 = echo "hello";
$msg2 = echo "must login";
$msg3 = echo "hello Marketing";
$msg4 = echo "hello Admin";
}
function getloggedin($userloggedin)
{
if($_SESSION['userlogin'] == ''){
echo $msg1 'userlogin';
} else {
header("Location: logout.php");
}
echo $msg2;
echo '<div id="'. $userlogin .'">';
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markting Home</title>
</head>
<header>
<div id="Menu">
<div id="<?php echo $userloggedin ?>" >
<?php //show only approved menu for what menu user is approved for ?>
<div id="<?php echo $menuOptions ?>" >
<div><?php echo $menu1?></div>
<div><?php echo $menu2?></div>
<div><?php echo $menu3?></div>
<div><?php echo $menuAdmin?></div>
</div>
</div>
</div>
<body>
<div id="content">
</div>
</body>
</header>
I'll be working on it all night learning as I go if you have any pointers that would great!
handpaintedstudio
- 13
- 8