Marshall Radziwilko
Marshall Radziwilko

Reputation: 25

Page Redirection

To help better explain my Question, here is what i have:

I am using a Precoded service, on it i have access to the Template HTML Files Only.

The URL to the Login page is : DynamicPage.aspx?Site=Mysite&WebCode=LoginRequired

The Main site URL would look like : DynamicPage.aspx?Site=Mysite&WebCode= or DynamicPage.aspx?Site=Mysite

What i need is a JavaScript I could put into the main header template file that would view "WebCode" And depending on whats entered redirect to a certain page.

I got from "Sitifensys" a code

Sitifensys

if (window.location.href!="foo.bar/login.php") window.location.href="login.php";

The problem with this code is even when i go to the main page it still redirects me to login.php which i do not want it to. I need this code to Read the "WebCode" If it is "LoginRequired" Redirect to "Login.php" else if redirect to "Test.php"

Hope this explanation is a bit better.

Upvotes: 1

Views: 211

Answers (5)

Mic
Mic

Reputation: 25164

You can add this script at the beginning of the body tag:

<body>
  <script>
    if((/WebCode\=LoginRequired/).test(window.location.href)){
      window.location.href = window.location.href.
        replace(/(\?|\&)WebCode\=LoginRequired/, '');
    }
  </script>
  ...
</body>

Upvotes: 0

sitifensys
sitifensys

Reputation: 2034

Just as answered earlier, you should add a window.location.href="somepage" somewhere.

Some where in your script :

if (window.location.href!="foo.bar/login.php") window.location.href="login.php";

This doesn't need to be in a listener for the page load event.

EDIT : Regarding your new descriptionn I would use (but would not recommend;)) something like :

if (location.pathname.indexOf("WebCode=LoginRequired")>0) {
    window.location.href="login.php";
}

Hope this will help.

Upvotes: 0

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

Don't do this that way. If you do anything in JavaScript it may be easily blocked by user. Better add and if() in your PHP code and then redirect to login, if user don't have specific session key:

if(!(isset($_SESSION['logged_in']) && true == $_SESSION['logged_in']))
    {
    header('Location: login.php');
    die();
    }

Upvotes: 2

JaredMcAteer
JaredMcAteer

Reputation: 22545

if (location.pathname.indexOf(login.php) >= 0) {
   //don't redirect
} else {
   /...
}

Upvotes: 0

Arihant Nahata
Arihant Nahata

Reputation: 1810

Try

window.location.href = "http://www.something.com/"

Upvotes: 3

Related Questions