How can you call a local web page with PHP - php

I'm sure this has been asked before, but I can't seem to find an answer. I'm working on PHP development, creating a basic login connected with mySQL database. Once the name/password is verified correct, I would like to show a welcome screen. My issue is that during development, I can't use a local directory like:
header("location:script/login_success.php");
From what I've read, header() is looking for an http:// URL. Is there any way to be able to test locally?
Thanks

header() can take any URL, even relative ones. If you are testing something locally, something like header("location: script/login_success.php")
just remember that header() must be called before any content is output or it will do othing

You can create a url for use in a header redirect by doing this.
$url = isset($_SERVER["HTTPS"])?"https://":"http://".$_SERVER["HTTP_HOST"]."/script/login_success.php");
header("Location: $url");
This should resolve correctly on localhost or elsewhere.
Another option would be to set up a constant that represents your base url per environment
define("BASE_URL", "http://localhost/");
and use that to build the url
header ("Location: ".BASE_URL."script/login_success.php");

Header has to be the first function call in page (you can access other functions but no output ones not even space or enter).. above everything(echo and html)... it should work everywhere...
When you echo the response is written partially and header is 200 ok... so... just write header first and then exit();
header('Location: url') ... I don't think url has to be relative... read on php.net

You can pass relative url to header there is no problem.
header("Location: /route/here.php");
is perfectly legit. Although the RFC dictates absolute URLs including the schema must be provided. Some web clients accepts the relative URL. Dagon pointed the RFC Matter.
14.30 Location
The Location response-header field is used to redirect the recipient
to a location other than the Request-URI for completion of the request
or identification of a new resource. For 201 (Created) responses, the
Location is that of the new resource which was created by the request.
For 3xx responses, the location SHOULD indicate the server's preferred
URI for automatic redirection to the resource. The field value
consists of a single absolute URI.
Location = "Location" ":" absoluteURI An example is:
Location: http://www.w3.org/pub/WWW/People.html
Note: The Content-Location header field (section 14.14) differs
from Location in that the Content-Location identifies the original
location of the entity enclosed in the request. It is therefore
possible for a response to contain header fields for both Location
and Content-Location. Also see section 13.10 for cache
requirements of some methods.

Related

Send a message with PHP header location redirect

Is it possible to include some message in a PHP header:
header("Location: http://somesite.com");
header("Message: hello");
then on site.com:
$message = some_function(); // "hello"
I am currently using a $_GET parameter in the URL, but looking for an alternative, maybe sending a $_POST?
I'm trying to not use $_GET, or use cookies (I know, those are the best ways..)
It sounds like you are wanting to send some extra data to the page you are redirecting to. No, this isn't possible outside of the query string. You should understand what is happening here.
When you send a 302 or 301 status code along with a Location: header, the browser sees this and then makes a separate request to the URL specified by the Location: header. The server isn't sending anything to that page. It's almost as if the user simply typed in that new URL in their browser.
I say almost because in some circumstances, there is a referrer set by the browser. This isn't guaranteed though.
What you can do is send some sort of token that contains more information. Perhaps your page saves off a message in a database or something, and then you pass the ID in the query string of the URL you're redirecting to.
Also, if you set session/cookie data and you're redirecting to something on the same domain, you can read that information on the page the user eventually lands on.
In addition to what Brad suggested, you can also send some info using # in the url without affecting the query string and then capture it with js.
header("Location: http://somesite.com#success");
in js:
if(window.location.href.indexOf('#success')>0) {
alert("operation successfully completed");
}

I can authorize users but cant redirect them to member page

I have designed a login page for my plugin it perfectly works and check the users credentials but when the user is authorized I cant move to the px_member234.php page.
the first page is px_myplugin234.php then it opens the px_login234.php file using include(px_login234.php) which is used for authentication, I suppose thats why the header does not work. is there any other option?
<form method="post" action="">
....
</form>
authorizer(){
........
if(user_is_authorized)
{
//go to member.php page
header ("Location:px_member234.php"); << does not work
}
}
I have also used the following but it does not work
echo '<script type="text/javascript">window.top.location="px_member234.php";</script>';
echo '<script type="text/javascript">window.location="px_member234.php";</script>';
This is because you're echoing HTML content before the header.
When PHP sees actual output, it has to flush all of it heads to apache (who sends them to the client). After PHP has sent the headers and started output, it is not possible to add more headers.
Your code needs to be refactored to call header('Location: ...') BEFORE outputting any content at all
You can use header redirection. To use this, you have to ensure, that nothing is outputed before the header command. If the header is already sent, this code wont work
header("Location:member.php");
In your example, the auth code have to be on the top of the page. If you can't prevent the output before your header function call, you could still add a meta redirect header to the html page or use JavaScript to set a new window.location
Besides calling the header() function before any output like others mentioned to the page you should also leave a space between Location: and the URL.
What you have:
header("Location:member.php");
What will work:
header("Location: member.php");
Update
Try and include the full URI instead of your relative one as per HTTP/1.1 Header Field Definition.
From php.net:
HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself.
To your form markup, you can add
<form method="post" action="?noheader=true">.
The ?noheader=true will stop Wordpress from outputting any headers. You can see details here: http://www.lessthanweb.com/blog/wordpress-and-wp_redirect-function-problem
Also, in wordpress development, wp_redirect is preferred over the php header function because it includes hooks that let it play nicely with other plugins.
Note that it takes the absolute url of where you want to redirect the user. Since you're making a plugin, you can use get_option('siteurl') to get the root url so:
<?php
wp_redirect(get_option('siteurl').'px_member234.php');
exit;
?>

PHP skipping script and click anchor tag

I have a form. After the form is filled out and the user clicks the submit button the user is taken to a thank you page. On the thank you page is a link (anchor tag) for the user to get to her home page. It works fine 19 out of 20 times.
The problem is, sometimes php skips the thank you page and goes directly to the home page. How is this possible? How is php clicking the link? I've gone over the code and it's completely correct. There is no javascript, just html and css.
Like I said, it doesn't do it every time and I guess it's not a bid deal I'd just like to understand what's going on. I'm using a simple header redirect like so
$url = "thanks/";
header("Location: $url");
exit();
What do you guys think is going? Is there any way to stop it?
Thanks
The RFC for the Location header requires a single absolute URI. This is also pointed out in the PHP manual in the notes section:
HTTP/1.1 requires an absolute URI as argument to » Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself
The problem may be to do with the fact that you're passing non standard headers to the browser. Browsers interpret the malformed header string differently and don't always behave as expected. Again as demonstrated in the PHP manual you should create an absolute URI, not an absolute or relative path before passing it to the header() function.
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;

Redirect to a different domain in PHP

I am trying to redirect www.xyz.com/my to www.abc.com, how can I do this in PHP?
Strictly speaking you need to send a HTTP location header to the clients browser.
To do this in PHP, as the other answers have mentioned is to use the header() function.
header("Location: http://www.abc.com");
There is a caveat that you should be aware of. The most common problem people encounter when dealing with HTTP headers is that they must be sent to the browser prior to any other data. If you echo any content to the client and then try to send the location header, it wont work.
For reference, there are many other HTTP headers that you should familiarize your self with.
You may do that in PHP by creating a subdirectory called /my on the http-root of the server where www.xyz.com is hosted and then make an index.php file with <?php header("Location: http://www.abc.com"); ?> inside it.
However, this is not the right way to do it. What you should do it create a .htaccess file right in the root folder of www.xyz.com looking like:
redirect /my http://www.abc.com
You can send a header redirect:
header("Location: http://abc.com");
If the default page for a directory is index.php, just put that code under /my/index.php. Any visits to the /my page will redirect.
header("Location: http://www.abc.com");
wrong technology. You probably want to look into apache htaccess, which is generally what most people use to host php.

Is it possible a Search Engine Friendly Redirct with Interface page?

I want to redirect page like: http://www.mysite.com/index.php?id=1 to http://www.mysite.com/the-real-name.htm but I don’t have the-real-name in first url then I should get it from db.
I made an interface page then I redirected page http://www.mysite.com/index.php?id=1 to it and I fetched the-real-name (with id parameter in url) from db then I redirected page to http://www.mysite.com/the-real-name.htm with PHP Header function.
Is this process search engine friendly?
Which page will be indexed with search engine crawler? Interface page or http://www.mysite.com/the-real-name.htm ?
Which is the best solution for indexing http://www.mysite.com/the-real-name.htm ?
Thanks a lot
If you want to tell the search engine that the final URL is "the URL", you need to do a permanent redirect. The HTTP status code is 301.
header('Location: http://www.mysite.com/the-real-name.htm', true, 301);
For the first redirect, you need to do a temporary redirect. The HTTP status code is 302.
header('Location: http://www.mysite.com/index.php?id=1', true, 302);
Keep in mind that it's good practice to not only send headers back for redirects, but a HTTP/HTML BODY as well that is shipping human readable information where the new location is. Redirects are not to be expected to be automatically performed by the client.
Different ways to implement
Depending on the system you work on, setting a HTTP status header with PHP might differ. The code above is for a working PHP version. Stick to latest. However, if you can not and the server integration is broken you might to push a bit the limits and force around a bit:
# Manually sending the HTTP 1/1 status line header - PHP does this nowadays, so normally not needed. But if you need it, ensure it's the first header you send.
header ('HTTP/1.1 301 Moved Permanently');
# Same here, but some CGI/FCGI+PHP implementations require you to set the Status header as well manually. Normally not needed.
header ('Status: 301');
# Set the Location header and status: (you will always need this)
header ('Location: http://www.mysite.com/the-real-name.htm', true, 301);
Always check if your script sends the correct headers by requesting it with a tool that is able to display the response headers not performing the redirect automatically, like curl:
$ curl -i "http://www.mysite.com/index.php?id=1"
Otherwise it takes a little long to wait for google to reflect the changes only for you to realize that you made some error.
When redirecting also set a 301 header and the search engines will know what to to from there.
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
If the relationship cannot change, use a permanent redirect as hakre suggested (a 301 status code). Otherwise, if that same id value might point somewhere else in the future, use a temporary redirect.
In either case, if the canonical (official, main, primary) URL is "http://www.mysite.com/the-real-name.htm", you can tell search engines that with a canonical meta tag in the page's head section:
<link rel="canonical" href="http://www.mysite.com/the-real-name.htm" />

Categories