Click to See Complete Forum and Search --> : php redirection
mahfooz
07-31-2006, 06:03 AM
hi to viewer
i want to write a function which each time a visitor clicks any links on my web page it simply checks that the sesssion is register or not.
if session is register then that visitor automatically redirect to the clicked page.
and if session is not register, it redirect it to the login page and after logged in that visistor redirected to that page on which he clicked earlier( before login )
shane.carr
07-31-2006, 05:18 PM
There's probably an easier way to do it, but you either have to go into htaccess or do this:
at login, add a value named "logged" to the session and ve it a value of "true" ( $_SESSION['logged']=true; )
change all of your links to the format href="/sessioncheck.php?page=my_orig_page.html"
make a file called "sessioncheck.php" in the root directory and add this code: <?php
session_start();
if($_SESSION['logged']=="true"){
header("Location: ".$_GET['page']);
}else{
header("Location: login.php")
}
?>
it should work like a dream. However, you're better of going into htaccess and set all requests to go to sessioncheck.php so you don't have to change every single link.
bokeh
07-31-2006, 05:32 PM
<?php
session_start();
if(empty($_SESSION['logged']))
{
header("Location: login.php");
die;
}
/*
page content here
*/
?>
shane.carr
08-01-2006, 01:54 AM
Or try NogDog's application:
http://www.charles-reace.com/PHP_and_MySQL/Login_Control/
mahfooz
08-02-2006, 01:28 AM
how to make .htaccess file and functionality can it perform.
shane.carr
08-02-2006, 11:22 AM
I forgot the exact code, but the best way to do it is probably using JavaScript to open up a new window to run the PHP to check for the session. The code will look something like this (it goes into the html file):
<script language="javascript">
<!--
var alllinks = document.getElementsByTagName("a");
var i = 0;
while(i < alllinks.length){
alllinks[i].onclick = function(){
window.open("/sesscheck.php", "_blank", "width=100;height=100;");
}
i++;
}
// -->
</script>
and then in sesscheck.php in the root directory
<?php
session_start();
if($_SESSION['logged'] != "true"){
echo "<html><body onload=\"alert('You are not logged in.');\"></body></html>";
}
?>
which will not block the person but it will tell the person that they are not logged in. This can be easily overruled of course with disabling javascript. If you want to actually block the person, you should try to play around with the javascript. I don't know much .htaccess code; that question should be put into the "Other" forum. Good Luck! :)