Forum Moderators: coopster
I'm total newbie to PHP and therefore I'm asking this question. I have a website that uses the free Login - Redirect v1.31 PHP script available from mpdolan.com to restrict access to some areas of the site only to registered users. The problem is that Google's robot is unable to access these pages as well. So I figured out that I should allow Google's robot to access these pages and that I'll recognize the robor by it's user-agent string. So in the already made code I want to add these:
1. Store the user-agent string in a variable (already did that by $user_agent = $_SERVER["HTTP_USER_AGENT"];)
2. Add to the if conditions that allow access in the existing code the condition: if $user_agent contains "Googlebot" than...
Here comes the question? How do I do the "If variable contains string "Googlebot" part?
In the past my website was in ASP and this solution worked just fine.
Thank you all in advance,
Goran
[us2.php.net...]
Fist of all feralo thank you very much. For some reason the script doesn't work. By doesn't work I mean doesn't allow access based on user-agent value. If someone could take a look in the code and tell me where I'm wrong I'd be grateful.
Here's the code that's on everypage that needs to be protected:
<?php
//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();
require('/home/.../var/www/html/secure/config.php');
require('/home/.../var/www/html/secure/functions.php');
//this is group name or username of the group or person that you wish to allow access to
// - please be advise that the Administrators Groups has access to all pages.
if (allow_access(Users)!= "yes")
{
include ('/home/.../var/www/html/secure/no_access.html');
exit;
}
?>
Here's the code of the config.php file:
<?
//set up the names of the database and table
$db_name ="db_name";
$table_name ="authorize";
//connect to the server and select the database
$server = "localhost";
$dbusername = "username";
$dbpassword = "password";
//domain information
$domain = ".somedomain.com";
//Change to "0" to turn off the login log
$log_login = "0";
//base_dir is the location of the files, ie [yourdomain...]
$base_dir = "http://www.somedomain.com/secure";
//length of time the cookie is good for - 7 is the days and 24 is the hours
//if you would like the time to be short, say 1 hour, change to 60*60*1
$duration = time()+(60*60*24*30);
//the site administrator\'s email address
$adminemail = "admin@somesite.com";
//sets the time to EST
$zone=3600*+2;
//do you want the verify the new user through email if the user registers themselves?
//yes = "0" : no = "1"
$verify = "0";
//default redirect, this is the URL that all self-registered users will be redirected to
$default_url = "http://www.somedomain.com/secure/loggedin.html";
//minimum and maximum password lengths
$min_pass = 6;
$max_pass = 15;
$num_groups = 0+2;
$group_array = array("Users","Administrators");
?>
And here's the content of the functions.php file which is the most important:
<?php
//Assign User-Agent value into $user_agent variable. I added this one.
$user_agent = $_SERVER["HTTP_USER_AGENT"];
//function to get the date
function last_login()
{
$date = gmdate("Y-m-d");
return $date;
}
//function that sets the session variable
function sess_vars($base_dir, $server, $dbusername, $dbpassword, $db_name, $table_name, $user, $pass)
{
//make connection to dbase
$connection = @mysql_connect($server, $dbusername, $dbpassword)
or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)
or die(mysql_error());
$sql = "SELECT * FROM $table_name WHERE username = '$user' and password = password('$pass')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
//get the number of rows in the result set
$num = mysql_num_rows($result);
//set session variables if there is a match
if ($num!= 0)
{
while ($sql = mysql_fetch_object($result))
{
$_SESSION[first_name] = $sql -> firstname;
$_SESSION[last_name] = $sql -> lastname;
$_SESSION[user_name] = $sql -> username;
$_SESSION[password] = $sql -> password;
$_SESSION[group1] = $sql -> group1;
$_SESSION[group2] = $sql -> group2;
$_SESSION[group3] = $sql -> group3;
$_SESSION[pchange]= $sql -> pchange;
$_SESSION[email] = $sql -> email;
$_SESSION[redirect]= $sql -> redirect;
$_SESSION[verified]= $sql -> verified;
$_SESSION[last_login]= $sql -> last_login;
}
}else{
$_SESSION[redirect] = "$base_dir/errorlogin.html";
}
}
//functions that will determine if access is allowed
function allow_access($group)
{
if ($_SESSION[group1] == "$group" ¦¦ $_SESSION[group2] == "$group" ¦¦ $_SESSION[group3] == "$group" ¦¦
$_SESSION[group1] == "Administrators" ¦¦ $_SESSION[group2] == "Administrators" ¦¦ $_SESSION[group3] == "Administrators" ¦¦
$_SESSION[user_name] == "$group" ¦¦ strstr($user_agent,'Googlebot'))
{
$allowed = "yes";
}else{
$allowed = "no";
}
return $allowed;
}
//function to check the length of the requested password
function password_check($min_pass, $max_pass, $pass)
{
$valid = "yes";
if ($min_pass > strlen($pass) ¦¦ $max_pass < strlen($pass))
{
$valid = "no";
}
return $valid;
}
?>
I also added the strstr($user_agent,'Googlebot')) part in the function that will determine if access is allowed.
What did I do wrong?
Thank you all in advance,
Goran
Now the function that allows or denies access contains the following but I'm still unable to make it allow access according to user-agent string:
//functions that will determine if access is allowed
function allow_access($group)
{
if ($_SESSION[group1] == "$group" ¦¦ $_SESSION[group2] == "$group" ¦¦ $_SESSION[group3] == "$group" ¦¦
$_SESSION[group1] == "Administrators" ¦¦ $_SESSION[group2] == "Administrators" ¦¦ $_SESSION[group3] == "Administrators" ¦¦
$_SESSION[user_name] == "$group" ¦¦ stristr($user_agent,'Googlebot') === TRUE)
{
$allowed = "yes";
}else{
$allowed = "no";
}
return $allowed;
}
Any ideas?
Thank you all,
Goran
If you have declared the $user_agent variable outside of the function, you need to declare it globally inside the function:
function allow_access($group)
{global $user_agent;
if ($_SESSION[group1] == "$group" ¦¦ $_SESSION[group2] == "$group" ¦¦ $_SESSION[group3] == "$group" ¦¦
$_SESSION[group1] == "Administrators" ¦¦ $_SESSION[group2] == "Administrators" ¦¦ $_SESSION[group3] == "Administrators" ¦¦
$_SESSION[user_name] == "$group" ¦¦ stristr($user_agent,'Googlebot') === TRUE)
{
$allowed = "yes";
}else{
$allowed = "no";
}
return $allowed;
}
dc
Post any errors you may receive
eelix
First, thank you. This is what I get when I add that:
Notice: Use of undefined constant Users - assumed 'Users' in /home/virtual/site271/fst/var/www/html/contents/photoschool/lesson001/index.php on line 16
Notice: Use of undefined constant group1 - assumed 'group1' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 60
Notice: Undefined index: group1 in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 60
Notice: Use of undefined constant group2 - assumed 'group2' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 60
Notice: Undefined index: group2 in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 60
Notice: Use of undefined constant group3 - assumed 'group3' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 60
Notice: Undefined index: group3 in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 60
Notice: Use of undefined constant group1 - assumed 'group1' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 61
Notice: Undefined index: group1 in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 61
Notice: Use of undefined constant group2 - assumed 'group2' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 61
Notice: Undefined index: group2 in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 61
Notice: Use of undefined constant group3 - assumed 'group3' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 61
Notice: Undefined index: group3 in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 61
Notice: Use of undefined constant user_name - assumed 'user_name' in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 62
Notice: Undefined index: user_name in /home/virtual/site271/fst/var/www/html/secure/functions.php on line 62
Lines 61 and 62 is the function that determines if access is granted that I posted above.
I'm not getting errors. It's just that the access is not granted and I'm trying to get the script let me (or Googlebot) in if I have the Googlebot user-agent. I'm using Firefox with the user-agent switcher addon to simulate Googlebot.
Thank you all so much,
Goran