(PHP) header(Location: ...) - php

Is there a way I can change the header instead of adding on?
For example
Creating a user
create_user.php (a form to action.php) -> action.php (switch case which will call another function) -> user_controller.php
So from user_controller.php, upon successful creation, i used the header method to bring the user to another location, view_user.php.
So now the url header is like
http://localhost/dct/action.php/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
but i want it to be
http://localhost/dct/web/view_user.php?&success=[SUCCESS]%20User%20is%20created!
without the action.php. How do i go about achieving that?

You will need to use URL rewriting - this depends on your setup. You can either do it at the web-server level, or application-level.

Two ways :
save your "base_url" website (http://localhost/dct/) in parameter and call header('Location: ' . $baseUrl . '/veiw_user.php?...)
Simply add a / before your URL (example: header('Location: /view_user.php?...'), and it will redirect from the root of your website, but in your case, it will redirect to http://localhost/view_user.php?....
The first way is better for your case, but you have to update the parameter when you publish your website in production environment.

Related

php header url as a variable obtained by $_GET["link"] or $_POST["link"]

I am trying to redirect page that can redirect crossdomain but I want to be able to change the destination domain as many times as i want without opening up and editing the php manually.
To give an e.g. Domain-A redirects to domain-B but I want to be able to change the target, domain-B to domain-C and so domain-A now redirects to domain-C. Can I do this via a form? I think I need to have the header as a variable but can't get it to work. something like: header("location:".$new_url).
You can use both a GET or a POST request. Here's an example with GET:
URL:
example.com/?redirect=new-domain.com
Code:
header("Location: " . $_GET['redirect']);

Call a URL before a redirect, via PHP head()?

I need to have 3 domains:
a.com - where our script (below) will reside.
b.com - has a tracking script we want to fire
c.com - is our final destination, like a landing page
So, an advertising ad creative can be linked to http://a.com/track.php?id=1004&r=c.com, where 1004 is my affiliate ID. When this script is called, I need it to do something like:
<?php
setcookie('affid', $_GET['id'],time()+60*60*24*365, '/');
head('X-Something: http://b.com/track?id=' . $_GET['id']);
head('Location: http://' . $_GET['r']);
...where X-Something is just some header that Apache understands and attempts to connect on before doing the page redirection.
Trouble is -- I don't even know what HTTP header field would allow this. Is it possible?
Ultimately an affiliate could make his advertising creative link to something like:
http://a.com/track.php?id=1004&r=c.com
It would drop a cookie on a.com, fire a script on b.com, and then redirect to c.com. Is that possible? If so, what header do I need to use instead of the made-up-one I created called X-Something?
I don't know that such an HTTP header exists.
Instead, another solution would be to make PHP request the tracking script. This could be done with file_get_contents(). The method will return you whatever that is available at that URL, but in this case you don't need it so its return value can be ignored.
<?php
setcookie('affid', $_GET['id'],time()+60*60*24*365, '/');
file_get_contents('http://b.com/track?id=' . $_GET['id'] );
head('Location: http://' . $_GET['r']);

How to deep link to a Facebook App (NOT Page Tab)

I need to link to a specific page in my Facebook app. The app is not in a page tab, and cannot be in one due to the project constrictions.
This is the url format:
https://apps.facebook.com/myappname
I would need to pass a parameter at the end (like /next.html or ?page=next) so that I can link to the specific page directly from outside the app (from an email).
How would I set this up? My project uses PHP and jQuery. I would love to be able to do this strictly in Javascript if possible.
I have found tons of info on how to deep link a page tab or a mobile app, but not to a regular application. I have found messages stating it's possible, but nothing about how to actually do it anywhere online or on Facebook.
Thanks for your help.
EDIT:
Okay, I got it working in PHP. For anyone else with this issue, this is what I did.
Add a "?" at the very end of the 'Site URL' in your FB app, then create a redirect file similar to this as your app landing page (just use absolute paths instead of relative ones like I did below):
<?php
$query = $_SERVER['QUERY_STRING'];
$params = explode("/", $query);
if (in_array("gallery", $params)) {
header("Location: /gallery.html");
exit;
}
else {
header("Location: /index.html");
exit;
}
?>
This answer is what helped me figure this out:
$_GET on facebook iframe app
I may be missing something here, but why don't you just link to http://apps.facebook.com/yourapp/something.php - this should automatically load your canvas URL, with something.php appended to the path
Obviously this won't work if your canvas URL points to a specific file and not a directory, but plenty of apps do this with success
When you are using the ? all you are doing is issuing a $_GET request, so all of the info you require will exist in the $_GET array.
Rather than query the $_SERVER array, query the $_GET array.
So if you had:
http://myurl.com?info=foobar
You can simply access that info using:
$info = $_GET['info'];
It is good practice to check for the existence first though:
if (isset($_GET['info']))
{
$info =$_GET['info'];
}
else
{
$info="default";
}
Incidently if you use the & character you can have multiple parameters:
http://myurl.com?info=foo&moreinfo=bar
You get a special parameter called app_data that you can use however you want. I've used it in the past to encode a full querystring of my internal app. for example, &app_data=My/Custom/Page
More found in this SO question: Retrieve Parameter From Page Tab URL

How to redirect a user from login page to another in php

Let's say a user has bookmarked "http://www.example.com/login#/settings". If that user try to access this page when he is logged out, firstly i want him to redirect to login page and then to the bookmarked page using this method
http://www.example.com/authenticate/login?continue=http://www.example.com/login#/settings
NOTE:
I'm using MVC architecture
Are there any method rather than HTTP_REFERER?
When user enter http://www.example.com/login#/settings ,i want to read whole url including # anchor in my controller file and then only i can set url to
http://www.example.com/authenticate/login?continue=http://www.example.com/login#/settings
so how do i do it??
You cannot read the part after the # from within PHP. You need to use JavaScript for that. For example, you can use
window.location.hash
To locate the hash-part, if any (it will be '' if no hash, or '#something' if there is one). You can then send this to the controller as a hidden field inside the request.
Depends on how you want to present it to your user. For a simple redirection, use header to send the HTTP Location Header
header("Location: http://www.google.com/");
If you want to give your user some time to read a short message before redirecting them, then you can use header to send HTTP Refresh Header
header( "Refresh: 5; url=newpage.php" );
Edit: In order to capture the anchor, you will need to use JavaScript. That information is not available to PHP. In that case, if you use JavaScript to capture the anchor, you might as well write your redirection in JavaScript.
Edit 2: Perhaps the other option is, when you are passing the continue to your program, also send the anchor as another GET variable. So your URI might look like this:
http://www.example.com/authenticate/login?continue=http://www.example.com/login&anchor=settings#/settings
Then use $_GET['anchor'] and concatenate it to the value of $_GET['continue'] with a #.
$uri = $_GET['continue'] . "#" . $_GET['anchor'];

What's the easiest way to redirect to the previous page with PHP?

I'm using a form to submit some post information to a PHP script. After the script finishes, I want it to redirect right back to the page the user came from. Right now I'm just using header() with a static URL. I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks.
Use HTTP_REFERER:
header('Location: ' . $_SERVER['HTTP_REFERER']);
access the $_SERVER['HTTP_REFERER'] variable and redirect to this. Should do the trick.
the way i would do it is use a session variable to store the current page URL everytime it is accessed.
$_SESSION['last_url'] = <get current url>
replace your static url in the header with $_SESSION['last_url']. Depending on how you implement your PHP, you can use search google for "current url php" or just $_SERVER['REQUEST_URI'] (stackoverflow doesnt allow me to put more than 1 link!)
Use REQUEST_URI But watch out for that presiding slash/

Categories