First put this on the page and see if session is auto started
Open a new file and add this code ( just this code) to the very top of the file (no spaces before)
PHP Code:
<?
$_SESSION['test']='testing';
echo $_SESSION['test'];
?>
Save it and visit it in a browser. If you dont see 'testing' then you need to add this
PHP Code:
<?
session_start();
$_SESSION['test']='testing';
echo $_SESSION['test'];
?>
Then you should see 'testing' and the SESSION is good. if you need to start the session the you need to do that on every page using SESSION variables.
Open a new file and add this code.
PHP Code:
<?
$kick_to="/index.php";
if(!isset($_SESSION['over18'])) header("Location: $kick_to");//kicks user to root folder index file. Change to anything you need
?>
Save this file as kick.php remember the path to where the file is saved
Open a new file and add this code to the top of every page you want to protect.
PHP Code:
<?
include "/path/to/kick.php";//change to the real path
?>
<html>
<head>
etc, etc...
index.php is presumably your site's entry page and where I'm kicking users to. Here have the javascript that handles showing the pop up. I've used a simple confirm but ou could use a pop up, hidden div etc and submit a form that reloads the index.php page.
PHP Code:
<?
//unset($_SESSION['over18']);//uncomment to reset SESSION var
?>
<html>
<head>
<script type="text/javascript">
<!--
function agecheck(){
<?
if(@$_GET['ru18']=="yes")$_SESSION['over18']=1;
if(!isset($_SESSION['over18'])){
?>
var r=confirm("Are you over 18? If yes click OK")
if (r==true){
document.location="<?=$_SERVER['SCRIPT_NAME']?>?ru18=yes";
}else{
alert('No pron for you!');
}
<?
}
?>
}
//-->
</script>
</head>
<body onload="agecheck()">
<?
if(!isset($_SESSION['over18'])) exit;//no session var so stop displaying content.
?>
hello grown ups!
</body>
</html>
Back up your original index.php and save above code as index.php in your root folder.
Please be specific with any problems you have. There never too much details when posting a problem.
Bookmarks