Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


#279973 19-Nov-2020 16:15
Send private message

No idea what I'm doing wrong, but I don't really know how to do this properly and it has me stumped. So have a very basic single page app (everything is just served from index.php) with a menu and all that which just loads content in to the page as required with ajax calls.

 

As I make the ajax call, I update the query parameters in the URL to suit so that if the page is refreshed, it loads the correct content again - URL just gets built out like "www.example.com/index.php?page=foo&id=bar". This part of it works just fine for refreshes.

 

But then before each ajax call, I check the PHP session status to make sure the user is logged in, and if not, redirect them to the log in page. When I redirect them, I gather the current query string and append it to the login page url so it persists - the url for the login then just ends up like "www.example.com/auth/login.php?page=foo&id=bar". This redirect is done using javascript "window.location.replace". 

 

So then finally we do all the jazz on the login page (just within PHP) to check the credentials, and if all good, initialise the session, and redirect them to the index page with whatever query parameters were present in the login page URL - this redirect is done using PHP so we get 'header("Location: www.example.com/index.php?page=foo&id=bar")' (except obviously I fill it with variables to get the current query parameters).

 

But where it gets stuff up. So the redirect flicks me across to index.php which loads the basic page, and the javascript and all that. And then the first thing we do in Javascript is check the URL to see if there are parameters given for "page" and "id". If these are null/undefined, we just load a preset default. But if these are not NULL, then it should load whatever the values are - this however doesnt happen. Every time index.php gets loaded from the login redirect, there are no query parameters available for javascript to read - I just don't get why!

 

Do I need to think about stick the required parameters in to a cookie before sending it the login in the first place? Was kind of hoping to just do it all in the browser/PHP.


Filter this topic showing only the reply marked as answer Create new topic
itxtme
2102 posts

Uber Geek
+1 received by user: 557


  #2607244 19-Nov-2020 21:58
Send private message

What are you using to access the URL in javascript?  

 

I have never tried do anything like you are trying to but just mocked it up and it works.  In the console it shows the variables as a string.

 

test.php

 

 

test2.php

 

 

Result

 

 

 

 

Your approach seems quite convoluted swapping between Javascript and PHP.  I would either move the data into localStorage or hold it in the $_SESSION. 




chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


  #2607290 20-Nov-2020 08:12
Send private message

Yep using document.location.search to get the info - which like I say, works fine on just a normal page reload, but just not after the redirect done by PHP.

 

And quite happy to change the approach!... like I say, no real idea what I'm doing, just mucking around and seeing what works. Will look at localStorage, makes more sense I think to use Javascript rather than PHP as all the loading of content gets done that way. Thanks for the tip!


itxtme
2102 posts

Uber Geek
+1 received by user: 557


  #2607311 20-Nov-2020 09:04
Send private message

I wonder if your use of document.search is not correctly written for the checking on redirect.  The reason I say that is the above code shows it does work, so are you accessing the variables incorrectly?  Maybe share some code.  You will probably want to use jsfiddle because posting code on here is impossible hence the screenshots




chevrolux

4962 posts

Uber Geek
+1 received by user: 2638
Inactive user


  #2607418 20-Nov-2020 10:47
Send private message

localStorage was the answer to better persistence! Take PHP out of the picture and let Javascript do all the "url management" made things way simpler.


Filter this topic showing only the reply marked as answer Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.