Forum Moderators: coopster
<?php
session_start();
$newip = $_SERVER['REMOTE_ADDR'];
if (!isset($_SESSION['username']) ¦¦
empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) {
header ("Location:http://members.example.com");
}
?>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TITLE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
</body>
</html>
Add an else clause and then see what happens
This will tell you if you have a problem with the redirect not working or if the if test is not working.
At this point I like to put in some echo statements to do some debugging. My guess is that the $_SESSION['username'] checks are not doing what you think they should be doing.
<?php
session_start();
$newip = $_SERVER['REMOTE_ADDR'];
if (isset($_SESSION['username']) echo "username is set<br>";
if (empty($_SESSION['username']) echo "username is empty<br>";
echo "newip = $newip<br>";
if (!isset($_SESSION['username']) ¦¦
empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) {
echo "I should be redirecting";
header ("Location:http://members.example.com");
exit; // My other suggestion is to put in this exit statement
}
?>
Try the code below, which will only show the html when the if statement fails
<?php
session_start();
$newip = $_SERVER['REMOTE_ADDR'];
if (!isset($_SESSION['username']) ¦¦
empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) {
header ("Location:http://members.example.com");
}else{
?>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TITLE</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
</body>
</html>
<?php
}
?>
This is the message I receive when I follow Timotheos example
<?php
session_start();
$newip = $_SERVER['REMOTE_ADDR'];
if (isset($_SESSION['username'])) echo "username is set<br>";
if (empty($_SESSION['username'])) echo "username is empty<br>";
echo "newip = $newip<br>";
if (!isset($_SESSION['username']) ¦¦
empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) {
echo "I should be redirecting";
header ("Location:http://members.example.com");
exit; // My other suggestion is to put in this exit statement
}
?>
Your if test is saying if any (NOT ALL) of the following is true redirect, otherwise display the html:
* There is no username set for the session
* The username for the session is empty
* The ip stored in the session doesn't match that of the ip currently making the request
So why not try adding the following just above your if test to see what is going on:
print "<p>Username = {$_SESSION['username']}</p>";
print "<p>Session IP = {$_SESSION['ip']}</p>";
print "<p>Remote Addr = {$_SERVER['REMOTE_ADDR']}</p>";
We'd expect the first to print out a username and the second and third to print the same IP.
"; if (empty($_SESSION['username'])) echo "username is empty
"; echo "newip = $newip
"; if (!isset($_SESSION['username']) ¦¦ empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) { echo "I should be redirecting"; header ("Location:http://members.example.com"); exit; // My other suggestion is to put in this exit statement }?>
"; print "
Session IP = {$_SESSION['ip']}
"; print "
Remote Addr = {$_SERVER['REMOTE_ADDR']}
"; if (!isset($_SESSION['username']) ¦¦ empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) { header ("Location:http://members.example.com"); }?>
This is the message I receive when I tried your second example.
When I login I can view the page I want but, when I am not logged in I can still type the URL in and access it that way.
What I want to do is if I am logged in be allowed access to the page but, if I am not logged in that I would be redirected to the login page. Make Sense? Have I went about this the wrong way?
I couldn't see what the problem might be? Instead of this being in the HTML file I saved the whole page as PHP. Maybe that was why it wouldn't Parse before. Any suggestions for line 7?
<?php
session_start();
$newip = $_SERVER['REMOTE_ADDR'];
print "<p>Username = {$_SESSION['username']}</p>";
print "<p>Session IP = {$_SESSION['ip']}</p>";
print "<p>Remote Addr = {$_SERVER['REMOTE_ADDR']}</p>";
if (!isset($_SESSION['username']) ¦¦
empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) {
header ("Location:http://members.example.com");
}
?>
<?php
session_start();
$newip = $_SERVER['REMOTE_ADDR'];
if (!isset($_SESSION['username']) ¦¦
empty($_SESSION['username']) ¦¦ $newip!= $_SESSION['ip']) {
header ("Location:http://members.example.com");
}
?>