How to redirect users to index.html?

I want to verify that a viewer came into the site via index.html. I try this by passing a value (v=“A”) as in a form using GET. This is in the meta refresh on line 4.

./one/test.html should receive and test the variable and redirect to a third page if it is good (or if bad, to logout.html).

I have done this with PHP, I would like to do so with only html so that a server is not needed.

<!DOCTYPE html> <!-- index.html -->
<html>
  <head>
    <meta http-equiv="refresh" content="0; url='./one/test.html?v="A"'>
  </head>
  <body>
  </body>
</html>
=============================
<!DOCTYPE html> <!-- ./one/test.html Did the viewer come in the front door? -->
<html lang="en-US">
  <head>
<title>test</title>

			<script>
			<!-- Did the viewer come from index.html? -->
			if(isset($_GET['v'])) {
				$v=$_GET['v']
				if($v = "A") {
					<!-- Good test, come in. -->
					<meta http-equiv="refresh" content="5; url='./one/menu.html?v='&20'" 
					}
			}		
			<!--Wrong value! Send them packing. -->
			<meta http-equiv="refresh" content="5; 
				url='./one/logout.html
		</script>	
		
	</head>
	<body>

	</body>
</html>

Thank you.
– Fred

Is the " Ask about anything related to HTML and CSS, including web design tools like Sass and Bootstrap." Correct? The only answers I see are directly related to the curriculum.
– Fred

it is the correct sub-forum.

Thank you, @hbar1st .
I can do the test with .php. I am hoping to find a way with html only so that a server is not required. I was surprised that there was not one reply to my question in 6 days.
– Fred

factors that affect responses are: complexity of the question and how well it is explained.
The easier you make it for someone to quickly read and understand the issue, the more likely it is you will receive a response.

1 Like

Thank you, Hanaa. @hbar1st .
I have edited and re-written the question. Should I ask it as a new question?
Thank you.
– Fred

no it is not a good idea to create a new question.

Let me maybe ask you some clarification points.
What do you mean by “verify a viewer came into the site”?
Do you mean you want to know if someone is visiting the site from the base index.html file?

Thank you, Hanaa @hbar1st

The “viewer” would be the person clicking on a link, or entering a url into a browser, or trying to access a page by any means.

“into the site” would the path or manor used to open any page.

The only valid path would be through index.html.

I would place the test into every page. Trying to open a page with a browser without first opening index.html would cause one to be kicked out through logout.html,

– Fred

another point of clarification. You said you want to do this in html only? But obviously that is not possible. I see you included a script tag so you are okay with writing javascript to do this?

Yes, if necessary.
Is it Java or Javascript that is often blocked in browsers?
Thanks.

it is typically javascript that is included in the script tag (Java cannot be used).
Here is a method that you can use to redirect the users
https://developer.mozilla.org/en-US/docs/Web/API/Location/replace

Thanks again, Hanaa. My problem is not the redirecting.

My problem is testing for the $_GET value.

– Fred

is this answer online what you were looking for?
https://www.edureka.co/community/67778/how-to-access-get-directly-from-javascript

1 Like

Exactly, @camperextraordinaire. I am looking at @hbar1st 's second link, trying to understand it.

Yes, every page would have the test. I want this to 100% client side with no need for any connection. Believe or not, this would be on diskettes with several files marked hidden and read-only.

Thank you both.
– Fred

Thank you, @camperextraordinaire .
Yes, and anyone who really wants to can use DOS to get to the files. Those trying to use just a browser will have to work harder.

From index.html ==> ./one/test.html with several parameters (validating only one) ==>(hidden, read-only) ./two/menu (validating a new parameter ). Then on to hidden files in hidden folder with a new test.

Like testing for a session while only using the client side.

@camperextraordinaire , @hbar1st : Thank you for your help. I post this here in the hope that it might help someone else.
– Fred

<!DOCTYPE html> <!-- index.html -->
<html lang="en-US">
  <head>
    <title>index.html</title>
    <meta http-equiv="refresh" content="0; url=./one/test.html?a=0&b=7&c=2&d=9&e=4&f=1&g=M&h=6&i=5">
  </head>
  <body>
  </body>
</html>
==========================================================
<!DOCTYPE html> <!-- ./one/test.html Did the viewer come in the front door? -->
<html lang="en-US">
  <head>
		<title>test</title>
 		<script>
			var parts = window.location.search.substr(1).split("&");
			var $_GET = {};
			for (var i = 0; i < parts.length; i++) {
   			var temp = parts[i].split("=");
    		$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
			}
			if (parts[3] == "d=9") {
						window.location = "../one/menu.html";
				} else {
						window.location = "../one/logout.html";
						}
		</script>
	</head>
	<body>
	</body>
</html>

Thank you, @camperextraordinaire . This is perfect for my ./one/test.html
About the only thing I know aboutJavaScript is to copy and past examples and try to understand them. It was pounded into my head that JavaScripts were bad and I have not kept up with the changes over time.

I’m trying to understand your script enough to use it on other pages. I see this as “goto A or goto B”. How can it stay on the page?

I would only redirect to ‘./logout.html’ if paramD IS NOT “9”.

Thank you, @camperextraordinaire
This is over my head for now.
It appears that if there is nothing after the .html (no ?variable=value), the split does not progress and the page calling route.js is opened anyway.

I guess that for now I’ll just use my index.html >>> test >>> (good test) >>> menu in a hidden directory. I just don’t have the time now to try and understand JavaScript.
Ideally, I’d have to pass the variable name and expected value to the function from the calling page, then, only if the test fails, redirect to logout.
– Fred

@camperextraordinaire , consider ./two/three.html below.
If the link ( Three ) from my menu is used, the page opens.
If you enter the url of “./two/three.html” in the address bar, you will be sent to “…/one/logout.html” This is what I want.

Repeat the test for page …/two/two.html which calls “…/one/route.js”, the page will open! With no variable and value the script does not redirect to logout.htm as I wish.

<!DOCTYPE html> <!-- ./one/menu.html -->
<html lang="en-US">
  <head>
		<title>Menu</title>
 		<script>
			var parts = window.location.search.substr(1).split("&");
			var $_GET = {};
			for (var i = 0; i < parts.length; i++) {
   			var temp = parts[i].split("=");
    		$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
			}
			if (parts[3] !== "d=9") {
						window.location = "../one/logout.html";
						}
		</script>
	</head>
	<body>
		<h1>Menu</h1>
		<a href="../two/two.html?a=0&b=7&c=2&d=9&e=4&f=1&g=M&h=6&i=5">Two</a>
		<br>
				<a href="../two/three.html?a=0&b=7&c=2&d=9&e=4&f=1&g=M&h=6&i=5">Three</a>
	</body>
</html>
=================================================================
<!DOCTYPE html> <!-- ./two/two.html Did the viewer come in the front door? -->
<html lang="en-US">
  <head>
		<title>Two</title>
<script src="../one/route.js"></script>
	</head>
	<body>
	<h1>Two</h1>
	</body>
</html>
================================================================
  <!DOCTYPE html> <!-- ./two/three.html Did the viewer come in the front door? -->
<html lang="en-US">
  <head>
		<title>Three</title>
 		<script>
			var parts = window.location.search.substr(1).split("&");
			var $_GET = {};
			for (var i = 0; i < parts.length; i++) {
   			var temp = parts[i].split("=");
    		$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
			}
			if (parts[3] !== "d=9") {
						window.location = "../one/logout.html";
						}
		</script>
	</head>
	<body>
	<h1>Three</h1>
	</body>
</html>
===============================================================
and ../one/route.js is below:
===============================================================
(function() {
    const queryStrParts = window.location.search.substr(1).split("&");
    const queryStrLookup = queryStrParts.reduce((lookupObj, part) => {
      const [ param, value ] = part.split("=");
      return {...lookupObj, [decodeURIComponent(param)]: decodeURIComponent(value)}
    }, {});

    const paramD = queryStrLookup.d;
    const redirectPath = paramD !== "9"
      ? './logout.html'
      : 
    window.location.href = redirectPath;
})();
===============================================================
Thanks again,
-- Fred

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.