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
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


  #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
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


  #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
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





News and reviews »

Air New Zealand Starts AI adoption with OpenAI
Posted 24-Jul-2025 16:00


eero Pro 7 Review
Posted 23-Jul-2025 12:07


BeeStation Plus Review
Posted 21-Jul-2025 14:21


eero Unveils New Wi-Fi 7 Products in New Zealand
Posted 21-Jul-2025 00:01


WiZ Introduces HDMI Sync Box and other Light Devices
Posted 20-Jul-2025 17:32


RedShield Enhances DDoS and Bot Attack Protection
Posted 20-Jul-2025 17:26


Seagate Ships 30TB Drives
Posted 17-Jul-2025 11:24


Oclean AirPump A10 Water Flosser Review
Posted 13-Jul-2025 11:05


Samsung Galaxy Z Fold7: Raising the Bar for Smartphones
Posted 10-Jul-2025 02:01


Samsung Galaxy Z Flip7 Brings New Edge-To-Edge FlexWindow
Posted 10-Jul-2025 02:01


Epson Launches New AM-C550Z WorkForce Enterprise printer
Posted 9-Jul-2025 18:22


Samsung Releases Smart Monitor M9
Posted 9-Jul-2025 17:46


Nearly Half of Older Kiwis Still Write their Passwords on Paper
Posted 9-Jul-2025 08:42


D-Link 4G+ Cat6 Wi-Fi 6 DWR-933M Mobile Hotspot Review
Posted 1-Jul-2025 11:34


Oppo A5 Series Launches With New Levels of Durability
Posted 30-Jun-2025 10:15









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.