What is the best way to redirect to same page with new &_GET variables added.
I want to do something like Google done in Analytics, WMT...
Lets say user opens the page www.example.com, I would like to redirect that to
www.exaple.com?lang=en&uid=01845654&p=1.
Also, if someone enters www.example.com?p=2&lang=fr I would like to keep that variables, just add necessary one.
Should I do it in ControllerBase or in DI or somewhere else? And what is the pest proper way to do it?
You should be able to do something along the following lines from one of your controllers:
$destination = ltrim($this->di->get('router')->getMatchedRoute()->getPattern(), '/');
$queryString = 'lang=en&uid=01845654&p=1'
return $this->response->redirect($destination . '?' . $queryString);
For the problem of keeping incoming GET parameters and setting only those that have not been used in the request, you would have to check for them manually (e.g. using $this->request->get(PARAM_NAME)) and then build your query string accordingly.
Related
Very probably this is impossible, but I want to ask it, just in case.
I have a database in which I save some numbers (1, 2, 3...).
I have a .php page from which I read the numbers. I concat those numbers to a string for getting a full URL. For exmaple:
$intArticle = $row["article"];
$strURLBase = "example.com/index.php/"
$strLink = $strURLBase . $intArticle;
//And I get "example.com/index.php/1"
But now, there is an exception in which the URL points to an external website, so my code is not valid for now.
I know how to fix it in .php, but I would like to know if it is possible to make the redirect directly inside the URL, saving the properly string inside the database. For example:
$intArticle = $row["article"]; //In this case, the value of $row["article"] could be, for example, "http://www.externalweb.com"
$strURLBase = "example.com/index.php/" //This part should be ignore inside the URL
$strLink = $strURLBase . $intArticle;
//I would get "example.com/index.php/http://www.externalweb.com"
Is there any kind of instruction that I could write inside the URL (that I would save into the database and then I would concat to $strURLBase) that redirects to another URL? For example:
example.com/index.php/!$%&_redirec_to("http://www.externalweb.com")
I don't want to call any PHP function from the URL for the redirection. In fact any PHP code shouldn't be executed. Everything should be inside the URL.
Try this:
if (!is_numeric($intArticle))
{
header("Location: ".$strLink."");
exit;
}
// your site will continue here, if the article is a number
Ok so when somebody types this into the URL mywebsite.com/?s1=affiliateid
I want to take the affiliateid part out of the URL. Every affiliate will put a different username into the address.
Then I want to create a link will point to differentwebsite.com/?id=affiliateid based on the username typed into the address bar.
Now so far, I know that I have to have something like this will get that affiliate id
$aff_id = $_GET['s1'];
Then I can use that variable to create a link or just redirect it to the next page
differentwebsite.com/?id=$aff_id
My question is, where do I place this code at? $aff_id = $_GET['s1'];
Do I have to make a page called ?s1.php or something?
Assuming s1 isn't used anywhere else but just to create a link:
<?php
$s1 = isset($_GET['s1']) && !empty($_GET['s1'])
? $_GET['s1'] // it's populated, use the passed value
: ''; // default value in case it's not present
//
// Maybe check $s1 is indeed valid
//
$newurl = sprintf('http://differentwebsite.com/?id=%s', urlencode($_GET['s1']));
?>
Then you can output that link somewhere on the page, like:
New Url Here
urlencode will make sure that if s1 has characters like &, =, ?, / (or others) it won't break the integrity of the url.
If you want the concise approach:
<a href="http://differentwebsite.com/?id=<?= urlencode($_GET['s1']); ?>">
New Url Here
</a>
You could place $aff_id = $_GET['s1'] anywhere before you want to use $aff_id. I tend to put stuff like that at the top of the page.
Or, simply put. "differentwebsite.com/?id=$_GET['id']"
I would suggess you do a check to see if the id parameter exists in the URL before you try to use it. Maybe even make sure it is the data type you expect, integer, string, etc. So as when you redirect users, you don't send them somewhere else in a broken way.
If you are not using this for SQL then no SQL Injection could occur #BlackHatShadow.
Append the $aff_id that you get from mywebsite.com to the url of the new web site. Presumably, $newurl = "differentwebsite.com/?id=".$aff_id.
Edit:
Do I have to make a page called ?s1.php or something?
You need to make a page that the user will land on when they hit the url: www.mywebsite.com/
I assume you are running a web server that can process PHP code. The code can go into a file called index.php in your server's document root directory. If you don't know what this is, I suggest googling a "how to" guide for your specific server.
Get the value of "s1" from the url and store it in $aff_id:
$aff_id = $_GET['s1'];
If you want to pass this variable into another web site which accepts an "id" parameter, then you can simply append $aff_id to the new web URL and redirect the user there.
Redirect the user to differentwebsite.com and also sends the $aff_id from mywebsite.com to the other URL:
header('Location: http://www.differentwebsite.com/?id='.$aff_id);
I'm using Codeigniter in my new web application, and I have a form in page which sends data via post to a server and that server returns the users back to my website but with the parameters via get with ugly links like example.com/id=12&code=Xxxx
What I would like to do, if it's possible and I've been searching and I can't find is how to convert those ugly links into nice friendly links ( example.com/12/Xxxx )
Thanks
You can't have a GET form transform into a nice URL directly. They will automatically become ?key=val format.
Your best option is to have a redirect header to translate the GET to the nice URL's.
eg:
$firstPart = $_GET['myKey'];
$secondPart = $_GET['mySecondKey'];
header('Location: ' . $requestURL . '/' . $firstPart . '/' . $secondPart);
Get parameters are basically standard for any Web API's so you will probably find that you will end up using them a lot so rather than hack fixes for each and every API create a middle man.
Create a new folder at the same level as your index.php (usually your site root). call this folder something like middleman
Add the middleman folder to the list of items in your .htaccess file that should not be routed through the index.php
Now for every API you use you can create a new .php file in the middleman folder which transforms all requests to one that your site can understand.
You then point your external API's to http://yoursite.com/middleman/api_name.php
The reason for creating the middleman is that Code Igniter obliterates the get array so it gets removed before you can process the requests and format them into something meaningful if you try to do it inside Code Igniter so we must perform the transformation outside of Code Igniter's scope.
an example file might looks like this:
mysite.pingback.php
<?php
$from = $_GET['ip'];
$time = $_GET['time'];
$post = $_GET['id'];
header('location: ../mysite/pingback/'.$from.'/'.$post.'/'.$time);
?>
So very simple files. but it also means that when a change is made to the external API it does not affect how your Code Igniter app works as you simple have to change your middleman.
A 3rd party service that I'm using returns the users to a url like this:
site.com/something.php?id=XXX&something=abc....
Therefore, I need to be able to accept $_GET parameters for only one part of the site.
Is there a way to put a file outside of codeigniter's application directory which will do something like this:
<?
$id = $_GET['id'];
$something = $_GET['something'];
//Do something so codeigniter thinks this is a request to site.com/process/$id/$something
require('index.php'); //codeigniter's index.php file
?>
I remember using putenv() to achieve this in the past, but don't remember the details.
Not sure about putenv, but if worse comes to worse you can get the contents of the get array by exploding $_SERVER['QUERY_STRING']
get doesn't need to be enabled and you still have access.
Try just using a redirect:
<?php
$id = $_GET['id'];
$something = $_GET['something'];
header('Location: http://www.site.com/' . $id . '/' . $something);
As long as the initial something.php request is made directly to that file and not index.php, then CodeIgniter won't run (actually, that would depend on your .htaccess [or equivalent] file, so you may need to tweak it).
That's the safest way I can think to do it, and you won't have to break up CodeIgniter's program flow or enable query strings in your application, which may be unsafe.
Maybe I'm misunderstanding your architecture but couldn't you just enable GET for codeigniter..?
See $config['allow_get_array'] in the docs:
http://codeigniter.com/user_guide/libraries/input.html
And see $this->input->get() on the same page..
try
$this->config->set_item("allow_get_array",TRUE);
in your controller's constructor before parent::__construct();.
The security filtering function is called automatically when a new
controller is invoked. It does the following:
If $config['allow_get_array'] is FALSE(default is TRUE), destroys the
global GET array.
I an redirection (in some cases) from a controller to the error controller, action 'not-logged-in' using the redirector helper. My problem is the following: I want to pass an argument in the $_POST array (an URL from where the redirection happened) so the user will be able to return to that page after performing a login.
How can i place data in the $_POST array while using redirect helper?
Thank you ahead.
When you use the redirector with an internal redirect (ie. goToRoute) the paramters are passed along with it. Thus if you add your refferrer to the the request before you actually redirect:
// Assuming $request is a Zend_Controller_Request
$request->setParam('ref', $referrer);
// then use the redirector
then that variable will be passed along with the request upon redirect. So then you would need to check for/grab that variable from the request in the action youve redirected to and then set it as a hidden field in the form. Then when your form posts to your login action you can check again for a ref variable and on successful login redirect to that location.
Now if i were you i would not actually use the referral as the url but a serialized or json encoded array of the previous request's parameters. that way you can use goToRoute in this second instance as well.
Ofcourse if the redirection came form some sort of post action that contained sensitive data you wouldnt want to do this. In that case you would want to use the session as has been previously suggested.
Above all the best advice i can give is to look at the code of Zend_Controller_Router_Rewrite and Zend_Controller_Action_Helper_Redirector.
Not possible without some socket or Curl jiggery pokery.
Why not try using $_SESSION array in the same way?
Does it really matter if the user can see the redirection url in the address bar? i doubt they will care and i see it a few times on some top sites.
Passing control to the login page just feels more like a _forward than a _redirect, like it all belongs under the one action. Especially since you're coming right back.
_forward($action, $controller = null, $module = null, array $params = null)
Then, you can pass your originating location in $params as you'd like.
I'm pretty sure that you can't send POST when redirecting a person to another page. But maybe you can, and if so, I hope somebody proves me wrong here.
I'm not sure how you'd do what you want using Zend Framework, but I would suggest two ways how to do it in general. You can either send a GET variable, or use a session variable to store a back-URL.