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

It appears you want to verify a visitor comes to a particular page (./one/test.hlml) via index.html and not just the site. If it is the entire site, then each page would need a similar logic check.

I was able to use the code in the link suggested by @hbar1st to accomplish the task I reference above. You will just need to use JavaScript syntax instead of PHP as currently show in the code you posted above.

1 Like

Exactly, @RandellDawson. 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

Just keep in mind, this would not prevent someone from just appending the querystring parameter and value to access the pages you are trying to prevent direct access to.

2 Likes

Thank you, @RandellDawson .
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.

@RandellDawson , @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>

Just in case you do not always put the d=9 in the same part of the query string.

  <script>
    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"
      ? './menu.html'
      : './logout.html'
    window.location.href = redirectPath;
  </script>

Also, the above is written more like JavaScript instead of PHP.

1 Like

Thank you, @RandellDawson . 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”.