Keep Dynamic URL Content in URL on Each Page - php

I have a PHP website that I send users to via a Dynamic URL like this:
http://mwebsitehere.com/?gw=1
well the page I send them too, works great with the code I am using to do certain things if the Dynamic content is set in the url. But whenever they click on a link on the page, which are ALWAYS changing, the Dynamic Content in the url is completely gone... For instances:
Lets say they are on the homepage that looks like this http://mwebsitehere.com/?gw=1, and then they click on a link that looks like this http://mwebsitehere.com/new-page/. Notice the ?gw=1 is completely gone from the url.
Is there a way to keep the Dynamic Links on every page if the url has dynamic content.
Like if it were to say ?gw=2 could all the links they click on or url somehow keep ?gw=2 on every page. Or if it said ?gw=1 for it to do the same thing.
Any help would be appreciated! Let me know if I need to explain my question better. Thanks!
I am also using wordpress, just in case you know anything wordpress specific! Thx!

the only reason to have get variables ?gw=2 in the url is if they are needed for that page, if you are wanting them for all pages,
have your scripts check to see if it exists in the $_GET array or $_COOKIES array, if its in the $_GET array but not it in the $_COOKIE array then set it in the cookies. That way your script will still see it,by checking the cookies.
No sense in cluttering the url with variables that dont need to always be shown.

If you want the exact same variable passed to every page, why not use
$_SESSION['gw'];
or
$_COOKIE['gw'];
to store "gw".
Otherwise you would have to pass it on via each link as follows
For example on page http://mwebsitehere.com/?gw=1
Link

There are a few ways you can do this.
You may use $_SERVER['QUERY_STRING'] and put it in every single link in your page. It will keep your links always repeating the same query string that your current file is.
You should try storing data in sessions! Then you can carry data from a page to another. Take a look at the PHP manual.
Good luck!

Related

Change a PHP URL Variable with Ajax/Javascript

I have made up a webpage on a private server as part of a full website redesign. The page calls up files such as contact.php with AJAX code to show the contents within a DIV layer, so that the actual page is still index.php but looks like a contact page.
What I would like to do is have a function that, when such links to make the AJAX call are clicked, will dynamically change the URL so that if somebody clicks the Contact link the page URL looks like index.php?page=contact (as an example).
Facebook has the sort of system I am looking to usewhen you click on a photo from the news feed and then change to another photo from the same user/gallery. I don't want something that just changes the URL from a hash tag (#), as that just adds complication to the design, whereas one that puts a PHP variable into the URL simply means I can write a PHP code to include the file directly if the variable is set.
Thanks.
Have you considered using History.js?
It sounds like you're wanting to essentially use $_GET vars to dictate what's served on the page... kind of. Although it also looks like you're calling in that content via ajax based on this var, so php is mostly irrelevant in this scenario since it would be js parsing the url and deciding what to ajax in. History will do all of this for you.
It reads the url as a state and serves as you tell it to. Obviously it's not that simple, but if you read the docs it's pretty clear how to use it.
https://github.com/balupton/jquery-history

Append some html to redirected site php

Let's say I have a script, that redirects to another page based on your type of input.
Now that site you're redirected to already has a long query string. So what I'd like to do,
is append some html code to the end of the site without actualy sending GET or POST requests, let's say something like:
<?php
header("Location: redirectedsite.php");
//send extre html img for example
$html="<html><img scr='img.jpg'></img></html>";
?>
Is that even possible? I know about sessions and cookies, but I'd like to see if there are any alternatives.
Once the browser redirects to the other site, the body of your page gets ignored and ONLY the other site gets shown to the user.
So, unfortunately, what you want is not possible; if the redirected site is under your control you could conditionally add more contents based on a GET parameter, but it would still not work in the way you've described.
In fact, this would probably be a security nightmare if you could append HTML to any another website.
It's not possible in that manner no, sorry. If you control the destination site you could, as you say, set a session variable that would prompt the destination page to append the code, but there is no way to do it directly in the way you want. And neither option is possible if you don't control the destination.
Nope. When you do a header redirect, the browser will go there straight away. You can't add some HTML in between.
You would have to do that on the target page, or show a proper HTML page that then uses another kind of redirect (using the legacy <META> or JavaScript, or a combination of both along with a link that can be clicked manually).
Aside from appending it as a query parameter, or using session variables, you can't do this. Is there any particular reason you don't want to use sessions?

Get top-frame (address bar in browser) URL in PHP?

I'm sorry for my English, it isn't so good...
I need help with a simple PHP program: if the page's URL contain a specific word, the content is showed. If there isn't it, the entire page redirects. I would use this to prevent me from frames that other webmasters make to "steal" my website contents.
For this is ok.. But a friend of mine need to make a black list of specific words, not a white list. So if the URL contain a word, the entire page redirects. I tried using *$_SERVER['HTTP_REFERER']* but if the "thief" put the frame on an external website that is not in the black list and after he make a frame of the frame, the content is showed and the page doesn't redirect..
So I think it's like that: *$_SERVER['HTTP_REFERER']* could read just 1st level frame and not the main page (top frame).
I really need help with this, I can't make it with Javascript because we want to keep the code not visible.
If it is not possible, is there a method to pass a JS variable value to a PHP variable for my case?
Thank you in advance!
This is only possible by javascript if and only if the top level domain is the same as yours. If that is the case, you could make an ajax call to the server, passing along the url and then the server could determine whether or not to redirect.
Instead of showing what the code would look like, I'm much more inclined to persuade you against this approach. If you have content that you don't want people to steal, you should make your site non-public (i.e. users must login). If that is not possible, the content that you are worried about sounds like it shouldn't be on the internet.

How can I access GET parameters from a Facebook iframe tab using PHP?

I am creating a Facebook iframe application, and need to use some $_GET variables. This works fine for the application, using something similar to what is described in this question.
However, when I use the app as a tab inside of a facebook fan page, I can't figure out how to pass the variable, or retrieve it. The URL for the site looks like this:
http://www.facebook.com/pages/(Page Name)/(Page ID)?sk=app_(app ID)
And if I try to simply tack a &var1=val1 to the end it doesn't work (Which I didn't really expect it to...) at all.
Is there a way that I can get to the displayed file, the home.php, in the url so I can tack the get variable onto the end and use it? Right now, there's no way to make it so that a copy-pasted url directs to a specific page inside the application.
You need to pass JSON via the app_data parameter. See https://developers.facebook.com/docs/authentication/signed_request/ for details.

JavaScript - Basic URL rewrite + creating record in history

Here's the situation.
I have a site where clicking hyperlinks within a certain div makes a jQuery function get the content of a div from a separate page. Because of this, the URL don't change. I need it to change as well as writing an entry in history.
My pages are setup like this (not sure this is the smartest way of going though)
access.php (main logon)
new-user.php
forgot-pass.php
index.php
controlpanel.php
etcetc. Now, all of these pages are reachable on their own and are mainly identical and all contain a div called "container". When clicking links, the content from this div gets erased and the content from the coresponding div (container) gets loaded from the file of the URL (href). I'm terrible at explaining..
So basically, what I need is some javascript that picks up the href link address and just pastes it in the url bar at the same time as it creates an entry in history so the back and forth buttons work.
I plan on extending this in a while as well, translating query strings as well. But there are a few constant static pages I need to take care of first. Any help would be very appreciated! :)
You are not allowed to change the entire URL by JavaScript but you can use URL hashes. I recommend you the browser history plug-in. You can simply register a handler to react on URL changes and load your corresponding content via ajax.
Have you looked at the jquery address plugin? Look at the examples. Is this similar to what you want?
It's not possible with "normal urls" (we must wait for a new generation of browsers...)
But there is a "trick": playing with anchors.
A link like "same_page.php#anchor" does not reload the page, but act on both the history and the adress bar.
So, if instead of having url like "page.php?param=lorem", you could have "page.php#param=lorem", you have your solution :)

Categories