I want to apply check only a specific website domain can hit my site. I tried $_SERVER['HTTP_REFERER'] but i am getting the full url.But i just want the domain address like www.example.com not all the url. I tried to explode but in the start i am getting http:\\ so when i explode on \ this .It explode the http section as well. Is there any other way so i can get on the domain url.
if the url is http://www.example.com/test.php i only want http://www.example.com. I have also used explode but explode function explode the http/ section first.I want the url of another website who's hitting my website
$hitting_url=$_SERVER['HTTP_REFERER'];
$site_url=parse_url($hitting_url, PHP_URL_HOST);
if($site_url != 'www.example.com')
{
header('Location:'.$hitting_url);
exit();
}
I have fix the issue with parse_url.
Related
I have a domain name say for example : www.example.com
I would like to get a dynamic data using PHP that is after this domain name. Example : www.example.com/samsung.
This samsung shall be anything that a user comes from.
I want to get this samsung in my PHP. The major problem here is that when ever I open this www.example.com/samsung or www.example.com/vivo page the browser goes to vivo directory and throws a 404 error.
For now I have solved getting the data from this format : www.example.com/?samsung
<?php
$key = array_search('', $_GET);
echo $key;
?>
But I want to get rid of the ? and have a pure www.example.com/samsung type.
This will give you last part of url
$url = $_SERVER['PHP_SELF'];
$url_array = explode('/',$url);
$result = end($url_array);
$Cleaned_url = str_replace("?", "", $result);
echo $Cleaned_url;
UPDATE : Creating Seo url :
.htaccess File
RewriteRule ^([a-zA-Z0-9_-]+)$ your_page.php?p=$1 [L,NC]
In php file when linking to url.
I save urls in database in news_url column
post title
This setup will give you www.example.com/samsung and solve your 404 notfound problem with right setup.
Attetion : Creating seo urls with htaccess requires knowledge just copy paste wont work.
you can search on google : for how to create seo url with htaccess
This examples are working 100%.
Use parse_url. See also this answer
I want to get the correct URL with PHP without any error, my links example:
https://example.com/
https://example.com/search/
https://example.com/search/?q=test
https://it.example.com/
https://it.example.com/search/
https://it.example.com/search/?q=test
so i want to get all link if is https://example.com/ show example.com if is https://example.com/search/ show example.com/search/ if is https://it.example.com/search/?q=test show example.com/search/?q=test etc.. without any error. thanks
Looks like you need $_SERVER['REQUEST_URI']:
$link = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Have a look over the PHP documents too: http://php.net/manual/en/reserved.variables.server.php
This will return the HTTP Host without https, and will also get you the request_uri with query strings etc.
parse_url() will also give you each element, and then you can build up the string you need:
http://php.net/manual/en/function.parse-url.php
I am using ModRewrite as below to convert urls on my site to be SEO friendly:
RewriteRule user/(.*)/$ seo-url-user-by-name.php?username=$1
Now I am writing code for seo-url-user-by-name.php and am looking for a way in PHP to redirect to:
user.php?uid=<uid>
so that seo-url-user-by-name.php will essentially return the contents of user.php?uid=<uid> BUT without changing the address in address bar to user.php?uid=<uid>
How do I do that?
Simply include user.php in seo-url-user-by-name.php. To get the querystring right you have to overwrite the value in $_GET.
$_GET['uid'] = 'whatever you want';
include 'user.php';
You're going about it backwards. The only URLs your code should be outputting are the 'friendly' ones. Those are the urls that will appear in the produced HTML and what will show up in the user's address bar.
e.g.
Bad URL (URL #1)
This URL is fine (URL #2)
You should never output anything but URL #2's. It'll be your server's responsibility to convert that clean (and in real terms, non-existent) URL to whatever really is on the server. PHP itself should never care nor see the /user/foo URL. PHP will be invoked as /user.php?id=foo as usual, and go about its business as usual.
The remote user would never see that rewriting occurring, they'll just see a request go out for /user/foo.
I am building a site directory and I am having some trouble linking up to sites..
The directory currently stores the site domain in a table and calls it through a foreach loop listing 25 separate domains on the page, but when I click on the links I am greeted with
localhost/directory (my site root) /linkeddomain.com
Rather than just displaying linkeddomain.com
I put http://www. in front of the array call
href='http://www.".$row['siteurl']."
However this is useless for production because if anyone enters into their domain http://www.theirdomain.com it will come out as http://www.http://www.theirdomain.com
Does anyone know how to fix this issue?
Thanks in advance
Luke
Make sure that the base URL is always the full URL, including the scheme and a subdomain (if applicable).
So:
$base_url = "http://livesite.com";
$base_url = "http://localhost/john/customerX";
$base_url = "https://secure.livesite.com";
If all your links are prefix by the base URL you should be fine.
Note that in all URLs I left off the trailing /. You can chose to include it, just make sure you always do it in the same way - have a clear normalized form.
You can just check to see if you need to add http to the url or not.
if(!preg_match("^https?://", $url)){
$url .= "http://" . $url;
}
That will only add it if it is needed.
I have a directory named "goto" and a file inside called index.php. Currently the following is inside the index.php file:
<?php
$url = $_GET['url'];
header("Location: $url");
?>
At the moment to redirect to another URL I have to type this into the address bar:
http://mysite.com/goto/?url=http://google.com
I would appreciate it if you could tell me how I could change that URL so that I could redirect the user to a website by typing this into the address bar:
http://mysite.com/goto/http://google.com
Use mod_rewrite and .htaccess to rewrite http://mysite.com/goto/http://google.com as http://mysite.com/goto/?url=http://google.com
RewriteEngine On
RewriteRule ^goto/(.+)$ /goto/?url=$1 [L]
Depending on your server configuration you may need to include a / in your rewrite path (i.e., ^/goto/(.+)$).
Unless you want to become a malware hub, I would wholeheartedly recommend you not doing this.
If you wish to allow redirect in such a manner, using http://mysite.com/goto/google and then work out the domain from a whitelist of available, allowed, destinations.
You will need to parse the data which could be a little tricky because you have to differentiate the difference between your URL and the other URL.
My suggestion is to not do so because the second that header is launched you will not see the url and it be better for you to just pass it as a get statement or a post.
EDIT
If you're determined then parse_url() is what you want. :)
#ide's method would work ... but you could also have the PHP script examine $_SERVER['PATH_INFO'], which is how that part of the URL would get passed to the CGI script.
(although, if there's a question mark in there, you'll also have to either make sure it's URI encoded, or also get the QUERY_STRING; you'll also lose any part after a hash, but you'd have the same problem with your current scheme)