I want to do the following : when the user access www.mysite.com, i want the server dynamically fetches the content of another site (let's say www.othersite.com) and generate the same html output for www.mysite.com.
So when the user goes to www.mysite.com, he will see exactly the same as he would see on www.othersite.com. This need to work also with www.mysite.com/myfolder, www.mysite/myotherfolder and so on.
I know i could use a redirect on .htaccess to do that, but for study purposes i want do that using only PHP.
Is there a way ?
You can fetch the target site's html code f.e. with file_get_contents and then just echo it out:
<?php
$htmlContent = file_get_contents('http://www.example.com');
echo $htmlContent;
But this won't fix the links in it, f.e. when the page has a Click and you click on it on your mirrored site, it will point to a non existing script on your server.
You could replace all links with preg_replace to point to your script with a query parameter of the target.
Related
I don't really know how to describe my problem properly, but anyway, here goes:
What I want to to is prevent some of my url from changing when linking between pages.
Fx:
localhost/intro/index.php
Now the "index.php" part is what I want to rename, or hide, throughout all of my pages, if possible.
An example of what I want to do:
www.cvkweb.dk/wi/doku.php
Try clicking at some of the green links at the page, and you'll notice that the "doku.php" part in the url doesen't change..
But how do I do it? .htacces?
Thank y'all in advance!
In the shown example, they don't change the requested page, because they use one and the same page... Imagine this kind of structure and code:
index.php
<p>Hello world</p>
Go to 1
Go to 2 <!-- all the anchors are referring to self -->
<?php if(isset($_GET['id']) && $_GET['id'] == '1'): ?>
<p> You have opened link number one </p>
<?php endif; ?>
<?php if(isset($_GET['id']) && $_GET['id'] == '2'): ?>
<p> You have opened link number two </p>
<?php endif; ?>
Conditionally by what is requested via the querystring, the response is controlled.
Which I would tell is bad practice, and you will end up with one single page which is full of code. Even if you do includes, and so on.
You way to go might be url rewritting, but not the way you wanted, but some routing which will map certain words from the url to certain pages. However, it will be not so hard to reverse what site.com/page/id is referring to page.php?id=1
So completely not knowing what is the requested page to the end user is nearly impossible. I would suggest to drop this desire. And maybe google about single page applications
You don't need to touch the server configuration.
Just link to the URL you want to link to.
Then write code in index.php to read $_GET and return different content based on the values of your query string.
URL rewriting can't help you there. The browser will always display the URL of the page you're currently on in the address bar. Your server will return data based on the requested URL. Therefore: page == URL. You can't display different pages with identical URLs.
However, you can change the content of a page client side without needing to load a new page. This can be done by embedding HTML frames/iframes in the page, or by using Javascript to get content from different URLs and placing it in the current page.
The page in your example links to completely different URLs, it distinguishes pages based on the query parameter (?...). That's a different URL. The PHP script returns different content based on it. No URL rewriting involved at all.
You should read about $_GET method.
PHP $_GET Method
You can pass a parameter via URL and based on what parameter you pass, display a certain content.
Your question is very confused. 1Index.php` is a special case - it's usually defined as an option in the DirectoryIndex config on an Apache installation. But you are not asking about index.php. You don't say what webserver you are using. Looking at the page you linked, it is clearly a Dokuwiki installation.
There are very detailled instructions on how to implement URL re-writing in the Dokuwiki manual
What I want is that with a URL, my site has a URL structure like this, http://mywebsite.com/page/test.gif, the test.gif is a GET parameter, thanks to .htaccess, I undestand that I can display a regular website doing so. I also understand that I could display the image directly using a gif file PHP header. What I want is to know if there is a way to check if user is coming from direct access or if the link is embeded and then switch from one option to another.
Thanks
debug_backtrace() || die ("Direct access not premitted");
Add some HTML inside the die() command to display a webpage. I will go out on a limb and assume you don't want an entire webpage, just a message that indicates you can't use your images if requested.
Source: Here
So what I am trying to do is this:
On my server users can put there YouTube channel name. My php file will then parse the channel and output HTML code with results. What I am looking to do is for the users to be able to put a code on there website that till call on my website lets say youtubevideos.com/videos.php?channel=channelname my code will take that name and output the videos back to there site. much like Google ads I guess.
Any idea how that is done, other than an iframe, I figured that will be my last resort.
I think what I'm looking for if for them to put a JavaScript on there site that will render as the HTML code I'm pushing from my php file.
Thank you!
The receiver code which is on the server you target need to set a header like that :
"Access-Control-Allow-Origin:*"
So, if you provide a service which need to exchange with your server & your code, is it possible. If you can't edit the targeted code & the header is not setted, it'll be impossible
There would be two parts of this solution.
In the videos.php file on your server, you would implement the logic to scrape the data from the original site and format it in the way you want to show on the final website.
For the end user, you would give a code similar to this that they would have to paste in their php pages to display the content from your site.
$your_website_url="http://youtubevideos.com/videos.php?channel=channelname";
//Don't forget the http:// at the start.
echo file_get_contents($your_website_url);
If file_get_contents() gives a security error, you can use curl.
I hope that helps.
How to hide an iframe url in HTML source code.I have two applications one applications get an url of another application into its iFrame,so that it displays in its source code.I dont want to display another application url in the source code.
I think you would need to set the IFRAME URL via JavaScript. The Javascript could then be Obfuscated, so that the URL would not be in plain text... Please see the following link for the obfuscator:
http://www.javascriptobfuscator.com/Default.aspx
i.e. if it was jQuery...
$("#myiFrame").attr('src','http://www.google.com');
becomes:
var _0xc1cb=["\x73\x72\x63","\x68\x74\x74\x70\x3A\x2F\x2F\x77\x77\x77\x2E\x67\x6F\x6F\x67\x6C\x65\x2E\x63\x6F\x6D","\x61\x74\x74\x72","\x23\x6D\x79\x69\x46\x72\x61\x6D\x65"];$(_0xc1cb[3])[_0xc1cb[2]](_0xc1cb[0],_0xc1cb[1]);
You can't hide it per say, but you can run it through something like TinyURL so that anyone interested would need to go an extra step. Anyway, that's the only thing I can think of. However, if you are displaying that page in a frame, what's the harm in having the URL in the source code? There really isn't a good, foolproof way to prevent someone determined from finding out the location of that iframe page.
You can create a php script which uses curl to call the url through localhost, then use this script as your iframe source.
If you have an issue with relative links and sub-directories, you can put your curl script inside the sub-directory.
We use iframes to show ads on our site.
Iframes are used to allow us to keep the ad generation code and other site modules separate.
As we track ad views on our site, and need to be able to keep an accurate count of which pagetype gets what views, I must ensure that users can't simply copy-paste the iframe in which the ad is loaded onto another site. This would cause ad count to become inflated for this page, and the count would not match the view count of the page the iframe "should" be displayed in.
Before anyone says so: no I can't simply compare the page view count with the ad view count, or use the page view count * number of ads per page, as # of ads per page will not necessarily be static.
I need to come up with a solution that will allow ads to be shown only for iframes that are generated dynamically and are shown on our pages.
I am not familiar with PHP sessions, but from what little reading I have had time to do, the following seems to be to be an acceptable solution:
Add "s = session_id()" to the src of the ad's iframe.
In the code that receives and processes ad requests, only return (and count) and ad if s == session_id().
Please correct me if I'm wrong, but this would ensure:
Ads would only be returned to iframes whose src was generated alongside the rest of the page's content, as is the case during normal use.
We can return our logo to ad calls with an invalid session_id.
So a simple example would be:
One of our pages:
<?php session_start(); ?>
<div id="someElement">
<!-- EVERYONE LOVES ADS -->
<iframe src="http//awesomesite.com/ad/can_has_ad.php?s=<?php echo session_id(); ?>></iframe>
</div>
ad/can_has_ad.php:
<?php session_start(); ?>
if($_GET['s'] == session_id()){
echo 'can has ad';
}
else{
echo '<img src="http://awesomesite.com/images/canhaslogo.jpg"/>';
}
And finally, copied code with static 's' parameter:
<!-- HAHA LULZ I WILL SCREW WITH YOUR AD VIEW COUNTS LULZ HAHA -->
<iframe src="http//awesomesite.com/ad/can_has_ad.php?s=77f2b5fcdab52f52607888746969b0ad></iframe>
Which would give them an iframe showing our awesome site's logo, and not screw with our view counts.
I made some basic test cases: two files, one that generates the iframe and echos it, and one that the iframe's src is pointed to, that checks the 's' parameter and shows an appropriate message depending on the result. I copied the iframe into a file and hosted it on a different server, and the correct message was displayed (cannot has ad).
So, my question is:
Would this work or am I being a PHP session noob, with the above test being a total fluke?
Thanks for your time!
Edit:
I'm trying to solve this without touching the SQL server, as the rest of the site is very SQL intensive and I don't want to add load to the already creaky SQL server...
You shouldn't really put the session id in the url because it makes it vulnerable to referrer snooping. (If the user follows a link from a page with the session id in the url, they will be able to see the session id in the referrer header)
Rather than using an iFrame, you might consider making the request for the iFrame contents on the server (eg using curl) and printing it onto your page. That way there will be no way for anyone else to directly embed it.
You could also consider checking the referrer header before loading the iFrame (which should be the containing page's url) but it is very easy to fake referrer headers so this shouldn't be considered a 'good' solution.
In summary, I'd strongly suggest finding another way of putting the ads onto your page!