Address Bar not changing - PHP - php

I am using header('Location:') to redirect the user to another webpage. But, instead of overwriting the previous address on the address bar, the new one gets appended to the one already present. For example:
The address bar currently holds:
localhost/v2/admin
and there is header('Location:'.DIRADMIN.'login.php');
where DIRADMIN is a constant defined as
define(DIRADMIN,'localhost/v2/admin/');
Then, after the redirect, the new address bar would be
localhost/v2/admin/localhost/v2/admin/login.php
I thought it was because of the server, I am using. I was using Uniform Server Zero. But then I installed WAMP and the problem continues.
I am still a noob, I have no idea what is causing this and if the problem is because of mod_rewrite, then on both servers the module was active. I already checked some of the problems like
redirect-PHP header(Location:..)
among others. I even did a google search for this but to no end.
How can I solve this problem?
header_remove();
will not work as the first address was typed in manually and not set by header();

Without the http prefix, browser is trying to find the path relative to your current one
Add "http://" as so
header('Location: '.DIRADMIN.'login.php');
If you want URL to be relative to your domain root path, you can just add '/'
header('Location: /'.DIRADMIN.'login.php');
You should always try to use a relative path - That way, if you change your domain, your code still works.

Define
define('DIRADMIN','localhost/v2/admin/);
As
define('DIRADMIN','http://localhost/v2/admin/');

Related

How to properly create relative links

in my source code, this a button link like this:
then the web page's code is like this:
but when click the button, the url is:
http://localhost/personal/applications/mywebtest/install/?step=2
why "/personal/applications" is added in?
Edit: Let me start with...
How the href attribute works
Let's say, you are on a page http://example.com/foo/bar.html and you have a hyperlink there, it can be one of the three:
path/newpage.html - this is a relative path (no slash in front), which means it will take you to http://example.com/foo/path/newpage.html
/path/newpage.html - this is an absolute path (starts with a slash), which will take you to http://example.com/path/newpage.html
example.com/otherpage.html - this is still a relative path (as in the first example), it will take you to http://example.com/example.com/otherpage.html *
this is because the browser don't know if you mean domain example.com or directory with a dot in it example.com, so it's a standard to treat it as a directory.
http://example.com/path/newpage.html - this is an absolute URL (it starts with a protocol) - the browser don't need to do any guessing here, can take you straight to http://example.com/path/newpage.html
(this is assuming that the base is not set, please read rest of the answer or take a look at https://www.w3schools.com/tags/tag_base.asp)
Now the original answer
When you use relative links (links without domain name) in the <a> elements your browser needs a whole URL (with domain name) to fulfill such request when you click the link. So it takes the current protocol (http) and domain localhost and glues it with $_SERVER['PHP_SELF'] which is
The filename of the currently executing script, relative to the
document root. For instance, $_SERVER['PHP_SELF'] in a script at the
address http://example.com/foo/bar.php would be /foo/bar.php.
(https://secure.php.net/manual/en/reserved.variables.server.php)
So you can either create full urls for the href as suggested here https://stackoverflow.com/a/46359685/299774
href=<?php echo $_SERVER['HTTP_HOST']."/mywebtest/install"; ?>?step=2';"
But this can cause problems in case you want to save such HTML in the DB (for example it is a post in your blog) - then moving your app to a different domain would require changing contents in the DB and fill it with new domain.
So I would stick to relative values in href, you can accomplish that by setting base meta tag in your HTML: https://stackoverflow.com/a/6848509/299774
or by utilizing mod_rewrite or similar tool four your server, but it has been long since I was doing it + outside of scope of this question, but you can check popular frameworks (CakePHP, Laravel) how they do it.
(And they have to do it, because being able to move app between domains is a must: local testing, staging, production)
That's because PHP_SELF returns the path on disk to the current file. PHP itself is unaware of where/how the page is served. You will have to find another variable/strategy for your link. Also, if you really want to link to the same page, you could just use ?step=2.
you can use of $_SERVER['HTTP_HOST']
<input type="button" name="step" value="Continue to step 2 of 3"
onClick="location.href = '<?php echo (isset($_SERVER['HTTPS'])?"https" : "http")."://". $_SERVER['HTTP_HOST']."/mywebtest/install/"; ?>?step=2';"/>

Header Location not working in Php [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 3 years ago.
The following statement doesn't seem to work. I am not getting any error messages either.
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q="+$query);
I am using file_put_contents() in search.php file.
Is there a way to figure out what could be wrong?
change this:
+$query
to this:
.$query
Because:
+ is the concatenation operator for JavaScript, where as . is the concatenation argument for php
Also, the path you are sending seems to be incorrect. The parameters inside the header function should be a complete web address, for example starting with http:
header('Location: http://www.example.com/');
While your answer is using a local file path: "Location: /home/shaliu/Projects/Nominatim...".
Please try:
You can use:
header("Location: http://www.example.com/search.php?q=".$query);
exit();
Or if your search.php file is on root directory:
header("Location: /search.php?q=".$query);
exit();
It may help you.
put error_reporting(E_ALL);
use relative path.
change this with
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q="+$query);
In php for concatenation use ".","+" is used in javascript not in php
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
Please make Your path correct for file
After fixing + to ., as said previously by #Webeng.
If you are visiting your page via typing /home/shaliu/Projects/Nominatim/website/index.php in your browser, headers wont work at all, because you are not in web context, but in local file view context.
If you are visiting it via http://localhost/Nominatim/index.php, you will be in web context, but your header wont work, because you are technically sending redirection to path of http://localhost/home/shaliu/Projects/Nominatim/website/search.php, which probably does not exists at this location. Instead of that you should change it to: /search.php if your website/ folder is linked to http://localhost/ or anything adequate, depends on your environement configuration.
you can try this.I hope it will help
header('Location: http://www.example.com/search.php?q='.$query);
header( ) is used to send http headers (http://php.net/manual/en/function.header.php). It looks as though you are trying to access a php file using a local file path and not an http (web) address. "/home/shaliu/Projects/Nominatim/website/search.php" needs to be web accessible and that web address is what needs to be used in header() (see #Purushotham's answer).
If you are using
/home/shaliu/Projects/Nominatim/website/search.php
In this case your location is starting with / that means home folder (directory) should be inside root folder of the server. For example as a local server if we consider XAMPP then home folder should be inside C:\xampp\htdocs (in general) and if we consider WAMP then home folder should be inside www folder.
If your home folder is inside the folder where your current page is, then you should use
home/shaliu/Projects/Nominatim/website/search.php
No / (forward slash required).
Second thing is you need to replace + by . to concatenate the string.
SO, if your home folder is inside root directory of server then you should go with
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
Otherwise, you should go with
header("Location: home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
Try this code with a test "if the file exists":
if(is_file("/home/shaliu/Projects/Nominatim/website/search.php")) {
header("Location: /home/shaliu/Projects/Nominatim/website/search.php?q=".$query);
} else {
echo "Error!";
}
Remember: In php for concatenation use . (#shivani parmar)
Remember: If the header is called after any HTML element, the php returns error!
header("Location:https://www.example.com/search.php?q='<?php echo $get_id; ?>'");
Please try this php code.
if you use header in php first header and location then question mark your search get id.
You need to replace plus(+) by dot(.) to concatenate the string.
Try this one.
header('Location: http://www.example.com/search.php?q='.$query);
Aside from the obvious concatenation issue that's already been pointed out, it looks like this path /home/shaliu/Projects/Nominatim/website/search.php is a *NIX path; given the /home/<user> pattern it starts with.
If this is the case, and you are trying to open a file outside of the web server root like that, yours is most likely a permission's issue. Moving your search.php to a location accessible to the web server process and running chmod/chown on the file you are trying to have the web server process open may sort you out. In addition, you may have to specify which OS you are using just in case you also need to run a chcon.
Oh, I would have made this a comment, but it seems like I do not have the necessary rep to add comments.

PHP header location absolute URL

I came across an issue where an absolute path set in header location wouldn't work but pointing to the file itself did. This only affected a few customers. One of them was nice enough to try connecting through a VPN which made the header location work.
Didn't work:
header('Location: http://www.example.com' . $_SERVER['PHP_SELF']);
Works:
header('Location: ' . $_SERVER['PHP_SELF']);
Can anyone shine some light on this?
Thanks
Your affected customers are not able to resolve the http://www.example.com (or whatever it actually is) URL for some reason. You can verify this by having them just try and visit the http://www.example.com by manually typing it in the browser location bar. That should fail too.
This can happen you have a site that is available under a number of domains, or directly by the IP address. Even www / non-www versions can make this happen. They hit the site at one domain or IP address that works for them, and then you try and redirect them to a URL they can not resolve. This explains why redirecting to just the path works, but an absolute URL doesn't.
If they can reach http://www.example.com in the browser, but not by redirect, ask them to blow out the browser cache.
Also always exit the script afterwards because otherwise in my experience in certain circumstances code that comes after the redirect might still be executed. So a good example would look like this:
header("location:http://www.example.com/path/to/myfile.php");
exit;
Often you would use a server variable for this case:
$url = $_SERVER["HTTP_HOST"]."/path/to/myfile.php";
header("location:".$url);
exit;
Cheers!
Link answer: https://tousu.in/qa/?qa=1091514/

PHP: how to prevent domain from being appended to the server-FQDN by nslookup using gethostbyname()

I am using the following code to check to see if the dns entry exists for a domain (basically to validate the domain is real)...
$sdip = gethostbyname("www.nonexistancedomainsdlkmsaldsa.com.");
$sddomain = gethostbyaddr($sdip);
print "IP: $sdip\n";
print "Domain: $sddomain\n";
It is returning....IP: 67.215.65.132 Domain: hit-nxdomain.opendns.com
So I am assuming that it is in fact searching for www.nonexistancedomainsdlkmsaldsa.com.myhostname.com (not exactly sure on this).
I read the article on php.net about adding a . following the domain, so I tried this and it didn't change anything. Anyone understand why I'm getting this? Basically, here is what I am trying to do...
User enters domain name (no http/https)....it checks to see if domain exists, if it does, it continues my script, if it does not exist, it fails with an error message.
I want them to be able to enter ANY valid domain name and it check it. So any other solutions would be helpful if there is anything better. Basically I am using this as a validation to see if domain name exists.
I would also (if at all possible), like to find out how to check regexp for
sub.subdomain.domain.tld where sub and subdomain are not required (basically to see if format is a valid format). I have searched stackoverflow, but they don't get into the detail I need to allow sub.subdomain but not require them.
article on php.net:
If you do a gethostbyname() and there is no trailing dot after a
domainname that does not resolve, this domainname will ultimately be
appended to the server-FQDN by nslookup.
So if you do a lookup for nonexistentdomainname.be your server may
return the ip for nonexistentdomainname.be.yourhostname.com, which is
the server-ip.
To avoid this behaviour, just add a trailing dot to the domainname;
i.e. gethostbyname('nonexistentdomainname.be.')
EDIT
it isn't in fact returning my server IP (i overlooked that). So I don't know if that article is relevant to this problem or not. I will leave it just in case it is.
It sounds like you're using OpenDNS as your DNS server. When you query OpenDNS for a non-existent domain, they will redirect you to one of their own "search result" pages/servers. This is specifically for OpenDNS, other DNS servers should not behave this way and correctly return no result.

To convert an absolute path to a relative path in php

I would like to convert an absolute path into a relative path.
This is what the current absolute code looks like
$sitefolder = "/wmt/";
$adminfolder = "/wmt/admin/";
$site_path = $_SERVER["DOCUMENT_ROOT"]."$sitefolder";
// $site_path ="//winam/refiller/";
$admin_path = $site_path . "$adminfolder";
$site_url = "http://".$_SERVER["HTTP_HOST"]."$sitefolder";
$admin_url = $site_url . "$adminfolder";
$site_images = $site_url."images/";
so for example, the code above would give you a site url of
www.temiremi.com/wmt
and accessing a file in that would give
www.temiremi.com/wmt/folder1.php
What I want to do is this I want to mask the temiremi.com/wmt and replace it with dolapo.com, so it would say www.dolapo.com/folder1.php
Is it possible to do that with relative path.
I'm a beginner coder. I paid someone to do something for me, but I want to get into doing it myself now.
The problem is that your question, although it seems very specific, is missing some crucial details.
If the script you posted is always being executed, and you always want it to go to delapo.com instead of temiremi.com, then all you would have to do is replace
$site_url = "http://".$_SERVER["HTTP_HOST"]."$sitefolder";
with
$site_url = "http://www.delapo.com/$sitefolder";
The $_SERVER["HTTP_HOST"] variable will return the domain for whatever site was requested. Therefore, if the user goes to www.temiremi.com/myscript.php (assuming that the script you posted is saved in a file called myscript.php) then $_SERVER["HTTP_HOST"] just returns www.temiremi.com.
On the other hand, you may not always be redirecting to the same domain or you may want the script to be able to adapt easily to go to different domains without having to dig through layers of code in the future. If this is the case, then you will need a way to figuring out what domain you wish to link to.
If you have a website hosted on temiremi.com but you want it to look like you are accessing from delapo.com, this is not an issue that can be resolved by PHP. You would have to have delapo.com redirect to temiremi.com or simply host on delapo.com in the first place.
If the situation is the other way around and you want a website hosted on delapo.com but you want users to access temiremi.com, then simply re-writing links isn't a sophisticated enough answer. This strategy would redirect the user to the other domain when they clicked the link. Instead you would need to have a proxy set up to forward the information. Proxy scripts vary in complexity, but the simplest one would be something like:
<?php
$site = file_get_contents("http://www.delapo.com/$sitefolder");
echo $site;
?>
So you see, we really need a little more information on why you need this script and its intended purpose in order to assist you.
This would be a lot easier to do in the HTTP server configuration. For example, using Apache's VHost
I'm not really sure what you're going for bc this doesnt look like absolute path to relative path, but rather one absolute path to another.
Are you always trying to simply change "www.temiremi.com/wmt/" to "delapo.com"? If thats the case, you just want simple string replacement rather than $_SERVER variables or path functions.
$alteredPath = str_replace("www.temiremi.com/wmt/", "delapo.com", $oldPath);
OR
$alteredParth "www.delapo.com/" . basename($oldPath)
If i misunderstand please explain, I don't know if you need this to be more robust/generic, and you kind of threw me for a loop with "dolapo.com" (when i first thought your title, i originally thought of comparing path to a value from $_SERVER and removing common parts,)
And as mentioned, if you are just trying to make the URL displayed the the user (in the address bar or links) look different PHP can't do this.

Categories