Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Detect if link opened in new tab or window or direct url access

    I was wondering if anyone know how to achieve this.

    I have an index.php that loads a file page.php into a div using jQuery. This page.php has a link that when you click it loads page2.php into the same div(so the url always stays as index.php).

    I want to make it so that, if you open the link on page.php in a new tab or window, instead of loading page2.php, it redirects to index.php(with page.php in the div). Same if you type in the link to page2.php directly into the browser.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    There is no way to detect this in PHP, or in JS (that I'm aware of anyway) either. Since HTTP is stateless, it doesn't have a mechanism in place to guarantee any relationship from request to request, so on the server end it hasn't a clue if this new request is related to a previous request or not.
    Using an .htaccess or webconfig should be able to get-ish around that though. If you are filtering all requests through a central script such as index.php, you can simply detect if the request is not index.php and use rewrite to send it back to index.php and add any additional information into the querystring for it. Problem here is that it'll apply across the board by default, so if you've already written it to work with the JS, that would need to be handled as well (or if it returns all data regardless, than it won't really need to do anything special).
    All and all there really isn't a problem here though. If you actually have a script under page2.php, it should be taking care of itself or dealing with any faults (such as sending it back to another page). Since these are directly accessed using JS, detecting faults isn't really an option since there isn't actually a fault, so all and all sending it back to index.php would really be more of an aesthetic purpose than functional I'd presume?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    No, there is a functional purpose, as I am implementing a game, and I don't want to enable players to be able to jump to any location just by typing in the respective php page into the browser......What I'm trying to accomplish is something like what they do in www.kingdomofloathing.com

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I won't be able to follow that link since I'm at work and my barracuda will blocks it for sure.
    In a stateless environment like HTTP paired with PHP, stopping such a thing is a matter of handling your data properly. I'd assume what you're looking at is cell traversal, so moving from one area to the next, and then the next, etc. But if they have open two windows, than they could technically go to: A. . . B. . . C . . .D . . . E and then break it by going directly back to 'B' by holding open a window from A. This is easy to get around in PHP since you already know where the user is, either tracked in persistent storage or in session. You simply evaluate that there is no route from 'E' back to 'B', so therefore the chosen option is no good, and you track them back to 'E' and inform them that the movement was a failure.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Using a session is probably the simplest way to achieve this - storing the current location and all the other info about the person's current game state in session variables.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    But if they have open two windows, than they could technically go to: A. . . B. . . C . . .D . . . E and then break it by going directly back to 'B' by holding open a window from A.
    Apart from preventing them accessing locations directly, this is indeed another problem that I'm trying to get around.

    You simply evaluate that there is no route from 'E' back to 'B', so therefore the chosen option is no good, and you track them back to 'E' and inform them that the movement was a failure.
    How would you write this in code?

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by pintee View Post
    How would you write this in code?
    If there is no path to page B from page E then in the code on page B check the session variable containing the current location before the move and if it is a page that has no access to page B then redirect immediately back to the page in that session variable. Only after performing that test would you reset the current location to page B.


    For example the following code at the top of page B will only permit it to be accessed if the previous page was A or C:

    Code:
    session_start();
    $validAccess = array('A', 'C');
    if (!in_array($_SESSION['currentlocation'], $validAccess)) {
       header("Location: http://www.example.com/".$_SESSION['currentlocation'].'.php'); 
       exit;
    }
    $_SESSION['currentlocation'] = 'B';
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    pintee (05-08-2013)

  • #8
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks. Will give it a shot.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Unfortunately, this probably won't be as simple to fix as felgall makes it out to sound.
    Think more of a Cartesian plane. I have positions of x,y coordinates, but in our situation I can only move through adjacent cells, which I'll call neighbors. Fortunately, figuring out neighbors if very easy to do if we assume that each step is equivalent to 1.
    So lets say I start at 0,0. I move right one step to 1,0. That is 'B'. Then I move up by one to 1,1 (c), up one to 1,2(d), right one to 2,2(e). Now I have to determine if I can go back to 1,0.
    In position 2,2 I have at most four neighbors, assuming I can only move in 90 degree angles. So my options are (3,2), (2,1), (2,3), and (1,2). So all I have to do is determine if I can go to cell with the coordinates of 1,0 (which is B), and I can see that (1,0) would not pass the check as being a valid neighbor.

    My approach is more of a data driven approach. I'd have a single 'page' which controls it all. Sessions would store current locations (2,2 for e), and querystring would determine desired location (1,0). Check if 1,0 is valid move from session, and if so execute whatever code is required and update the session to store the new coordinates.

    Edit:
    Also, this will prevent the use of direct typing or holding the window open. On a side note, persisting storage will end up being a requirement; if they terminate the session for any reason than you'll lose their location. Sessions are quick and easy to work with, but they are not persistent, so you may be needing to update coordinates within a database on each move.
    Last edited by Fou-Lu; 05-08-2013 at 01:32 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    New Coder
    Join Date
    May 2013
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'm not sure I see much of a difference between your approach Fou-Lu, and felgall. In both cases, you are traversing a graph, with each node having legitimate 'next moves'. I think, I will use felgall's solution since it makes more immediate sense(unless you, Fou-Lu, can be bothered to explain in more detail exactly how your approach differs and is better )

    I was thinking use felgall's method to prevent direct typing, and the method given by rip-off, here, to prevent inconsistency between multiple instances of the game open at the same time. I would be interested to hear your thoughts Fou-Lu as to whether my proposed approach will work.........

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The ideas are the exact same, but my approach would be data driven while felgall's is structurally driven. You need to create and modify a script for each cell in order to determine where to go. So you would literally have scripts A.php, B.php, C.php, E.php, and D.php, all with an array of information determining where it would go. Both of the above prevent inconsistencies between windows, but only to the point of throwing an error and returning (there is no way to push a forced update on a client with PHP).

    The thread you have posted would be suggesting the use of something like an access token. I give you the token, you submit it back in the form. I check if that token is consumed, and if not, I go ahead and consume the token and provide you with a new one for the next input. The concept is similar, but not reliable, to my data driven approach on location. The problem is (assuming I interpret this correctly), there is no control in place to govern the move itself as valid, only that the move is allowed from this request. Since you'll need to verify that the move is possible (otherwise I can just modify the move to on the client anyway), this then becomes redundant.

    Writing a function to check a valid move is very easy:
    PHP Code:
    function canMoveToCell($xTo$yTo$xCur$yCur$width$height)
    {
        
    $bValid true;
        
    $rangeX $width 2;
        
    $rangeY $height 2;
        if (
    abs($xTo) <= $rangeX && abs($yTo) <= $rangeY)
        {
            
    // within bounds
            
    if ($bValid = (abs($xTo $xCur) <= 1))
            {
                
    $bValid abs($yTo $yCur) <= 1;
            }
        }
        else
        {
            
    $bValid false;
        }

        return 
    $bValid;

    If you just use "up", "right", "left", "down" for example as buttons, than you can also calculate where they go from where they are. This limits the amount of input the client can provide. You still require validation checking though; if they hit the bounds of any location, they cannot move past it. Assuming if you do that, you'd also check that they cannot go "up" from this point, and as such they have no "up" as an option. But if an old window is still running, than they can hit "up". That is why you'd still need to check where you are.

    If you actually use a graph datastructure, which may not be the best idea since the memory will really start to balloon, graph's typically have a native method to both get their neighbours as well as to check if another vertex is a neighbour.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    pintee (05-08-2013)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •