If you want to make sure your website is not opened in a frame from some other website, put this JavaScript in the <head> of your html page:
<script type="text/javascript">
if (top.location != self.location) { top.location.href = self.location.href; }
</script>
Note that if someone loads page1 they can then go directly to page2 anytime in the SAME browser session. But if they close the browser, then next time they must again enter through the "front door".
<script type="text/javascript">
nRandom = Math.ceil(Math.random() * 99);
document.write ('<img src="image' + nRandom + '.jpg">');
</script>
<script type="text/javascript">
if (top.location != self.location) { top.location.href = self.location.href; }
</script>
Stop direct links to web pages
Want to stop someone from entering your site unless they come in the "front door"? Or want to keep a frame page from being loaded outside of the frameset? This script example will not let you load page2 unless you have already loaded page1 in the same browser session.Note that if someone loads page1 they can then go directly to page2 anytime in the SAME browser session. But if they close the browser, then next time they must again enter through the "front door".
page1.php has this script:
<?php
session_start();$_SESSION['valid'] = "yes";
echo "Page 1";?>
page2.php has this script:
<?php
session_start();
if ($_SESSION['valid'] != "yes") { header("Location: page1.php"); }
echo "Page 2";?>
Adding individual "members only" pages
By combining some of the Tips & Scripts on this page it is possible to make a simple "members only" section on your website. When a user logs in they are automatically sent to their page, and can not get to anyone else's page.
Use the "setting up password protection" tip to set up a password protected directory. Let's call that directory members. Under that members directory have a subdirectory for each member. For example members/joe and members/jane.
Then in that password protected directory members, use the "Passing user authentication information to PHP" to identify the user and route them to the directory with their name. So you have a page members/index.php which would contain a script something like this:
- - Start Script Here - -
<?php
$directory = $_SERVER['REMOTE_USER']; // directory name is the user nameif (file_exists($directory)) {
header("Location: " . $directory . "/"); // go to the user directory} else {
header("Location: http://yourdomain/error.htm"); // not found}?>
- - End Script Here - -
Then you need to secure each individual members subdirectory so no authenticated member can get to another member's subdirectory. You can do this with another .htaccess file which you put in each member's subdirectory. But there is no need for another identity verification. Simply put these three lines in a .htaccess file in each member's subdirectory (this example being for joe):
RewriteEngine On
RewriteCond %{REMOTE_USER} !^joe
RewriteRule .$ http://yourdomain.com/error.htm [L]
In this example, if anyone other than joe tries to access anything in that subdirectory they will get the error page.
In summary:- A member always goes to page domain.com/members. If the member is not logged in they will get the login prompt and then be sent to their page. If the member has already logged in, they will just be sent to their page.
- Using this method you must put each member name in the htpasswd file for authentication, use that name as a directory name, and put that name in the htaccess file for that directory. So it is not maintenance free - but it is very secure, easy and "light weight". Note that all this is case sensitive, Joe is not the same as joe. The .htpasswd member name must match the dirctory name which must match the name used in the .htaccess for that directory.
Load a random image
This example will randomly load image1.jpg through image99.jpg. Just place this JavaScript where you want the image.<script type="text/javascript">
nRandom = Math.ceil(Math.random() * 99);
document.write ('<img src="image' + nRandom + '.jpg">');
</script>
Comments
Post a Comment