I'm working on a survey system for my company and I have it setup in the system so that there are two ways to take the survey.
1) New Survey Taker no prior information
2)Survey was already sent out and a session was created.
In case one I would like my URL to look like:
mydomain.com/SurveySystem/index.php/survey/$surveyID
($surveyID being a integer of the survey to take)
The second case would where we create a link with for the test taker. I would like the URL to look like this:
mydomain.com/SurveySystem/index.php/survey/$surveySessionID/$guestID
In my Survey class I have it setup as the following:
function index(){
$segments = $this->uri->total_segments();
if($segments == 1){
echo "no surveyID set";
return;
}
if($segments == 2){
$this->take_survey($this->uri->segment(2));
}
if($segments == 3){
$this->survey_session($this->uri->segment(3), $this->uri->segment(4));
}
}
When no information is passed the it echos just fine.
But if I try to put a integer where the surveyID is it thinks i'm loading up a method in the controller.
Thank you for the help!
Use URI routing to override the default controller/function/arguments mapping.
Example: in your application/config/routes.php:
$route['survey/:num'] = "survey/take_suvey";
Bonus: You can also remove the index.php/ part, see Removing the index.php file.
My question is, with a URL that unfriendly, why do you care what it looks like at all? It is not semantic from a user perspective. Yes, you should be removing the index.php. Coupling the URL rewriting, the whole thing should be reduced from:
mydomain.com/SurveySystem/index.php/survey/????
to
mydomain.com/survey/
And your CI class methods can be reduced to "take," or "submit," and a subsequent "review."
Sessions should be managed using cookies or CI's session class. If you need to track state in the URI, combine your survey-specific "session" and the "guestID" into one segment with base64 encoding.
Lastly, using a route as suggested, there will be no way for your app to know what survey ID to load. It would need to capture the ":num" and feed it to take_survey:
$route['survey/(:num)'] = "survey/take_survey/$1";
If you take numeric ids as the first segment after /survey, you need another route placed after that one to handle the case where that segment is a session ID:
$route['survey/(:num)/(:num)'] = "survey/session_manager/$1/$2";
Where the $1 and $2 are session ID and Guest ID respectively. Personally, I would say this is bad form. The semantic meaning of your segments break down: it becomes difficult to determine what that first numeric segment means (is it a survey ID or session ID), unless you can always guarantee that these routes are in place.
Related
I want pretty url. I access these records using url below. Last segment is index id to access data using it.
http://www.example.com/order/thank_you/344
How can I convert it into
http://www.example.com/order/thank_you
How to implement and also I want last segment in page to access the data using it.
routes.php
$route['order/thank_you/(:num)'] = 'thanks/thank_you/$1';
The left hand side is the URL accessed by the users. And the right hand side is where your request going to.
For example, the request example.com/order/thank_you/344 is going to the function thank_you() of the Controller thanks.
$1 is the argument. If have more than one, just /$1/$2/.
The Controller
public function friends_profile ($id = NULL) {
echo "This is the ID I want to thank to " .$id;
}
The function arguments can be passed directly by the route. $id is the argument passed by the URI.
By the way, check out the Codeigniter URI Routing Guide to get more information.
If you want the url not to contain the id portion you could keep it in a session var and retrieve it at the thank_you method.
For example, I have a page called profile_page.php. This page is only functional if data is written after the ?u= in the URL, for example, data for Alice's profile page can only be seen when the URL reads http://localhost/profile_page/alice.
Loading http://localhost/profile_page will give me undefined variable errors as most of my variable's are depending on the URL to have a value after the ?u=. For example, the variable $firstname can only be gathered when I get her username in the URL.
In such a case, when http://localhost/profile_page, I would rather have it redirect the user to their own profile_page, but I don't know how I can test the URL and parse it through an if statement.
I understand you can use $u=$_GET['u']; to obtain the current page URL? but I don't think doing this, is the best way to go about it:
$u=$_GET['u'];
if ($u == "http://localhost/profile_page/"){
// redirect to logged in users page code here
}
First, if you are using some parameter for your page to build, the url would looks like httlp://localhost/profile_page.php?firstname=alice&lastname=brown, with $_GET['firstname'] you will get alice in this case. If you want to test if the parameter is set first and redirect to another page if it is not set, you could use
if(!isset($_GET['firstname'])
{
header('Location:redirected_page.php');
}
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.
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 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.