Welcome Guest, Not a member yet? Register   Sign In
Help using GET to read parameter in URL
#1

First off, I am running CI version 2.0.

I have a Bing ads campaign where I send visitors to my landing page. I want to make it so that when the person searches for "red widget" and then clicks on my ad, the landing page they are sent to contains the words "red widget" in various places I specify. Bing allows adding the keyword parameter to the destination URL which ends up looking like this:

mysite.com/ladingpage.php?kw=somekeyword

The new URL was causing this error: The URI you submitted has disallowed characters

I fixed this error by going into my config.php file and allowing ?&= characters on this line:

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-?=&';

Now when I am at mysite.com/ladingpage.php?kw=somekeyword the page doesn't work, because there is no such page. In order to get the page to load with a parameter in the URL, I made the following changes in my config.php file:

1. I changed   $config['uri_protocol'] = "REQUEST_URI";  to "PATH_INFO", which didn't work, sent all pages to a messed up version of my homepage. I finally got the page to load properly with the keyword param in the URL by changing it to "QUERY_STRING". Everything else seems to be working ok with it set this way but I have no idea if this is good practice or not.

2. $config['enable_query_strings'] = FALSE;   I set this to TRUE, but it seems to work even set to FALSE as long as the uri_protocol is set to QUERY_STRING

Now I have my landing page loading properly with a URL like mysite.com/ladingpage.php?kw=somekeyword. Now I just need to read the keyword from the URL and print it on the landing page. This is where I am stuck right now. Here's the code I am using:

PHP Code:
<?
if (isset($_GET['kw']))
{
$keyword = $_GET['kw'];
 echo $keyword; 

 ?>

Without the isset I was getting undefined variable errors all day long. Now I am not getting the error and the page is loading perfectly, but it is not printing the keyword on my page. Any help with this would be greatly appreciated.
Reply
#2

You could try using $this->input->get() rather thank $_GET

I'm not sure whether it will make any difference?

Code:
$kw = $this->input->get('kw');
echo $kw;
Reply
#3

Seem your URL doesn't follow CI URL (http://mysite.com/controller/method). In CI, you must define a controller to handle requests. Your URL can work on Web Server without CI, just use direct PHP code.
Reply
#4

(01-19-2015, 01:31 AM)flemming Wrote: You could try using $this->input->get() rather thank $_GET

I'm not sure whether it will make any difference?



Code:
$kw = $this->input->get('kw');
echo $kw;

(01-19-2015, 03:25 AM)pdthinh Wrote: Seem your URL doesn't follow CI URL (http://mysite.com/controller/method). In CI, you must define a controller to handle requests. Your URL can work on Web Server without CI, just use direct PHP code.

Thanks for the replies. Actually my urls are set up that way but I didn't mention that in my first post. My landing page is actually at mysite.com/details/landingpage. Also, in case this matters, I do not have a codeigniter.php file anywhere in my core folder or elsewhere on my server. I have been running this site for a few years without it and I haven't had any issues but figured I'd mention this since everyone is supposed to have it from what I have been reading online about CI. It's been that way since I acquired the website.

I just tried flemming's code in place of what I had on my landing page, and that didn't work. So I tried to put flemming's code into a function in controller that serves the landing pages but that also didn't work.

PHP Code:
function _ppckeywords($product)
 {
 
$ppckw $this->input->get('kw'); 
 
 return 
$ppckw;
 }
 
 
 function 
index($product)
 {
 
$data['ppckeyword'] = $this->_ppckeywords($product);
 
$data['prodname'] = ucwords(str_replace('_'' '$product));
 
$data['keywords'] = $this->_keywords($product);
 
$data['descript'] = $this->_descriptions($product);
 
       $data['title'] = $this->_pagetitles($product);
 
$data['product'] = $product;
 
$data['view2'] = $this->_view_two($product);
 
//echo '<pre>'; print_r($data); echo '</pre>'; exit(); 
 
$this->themes->view('promote2/landingpages'$data); 

I created the ppckeywords function and then added the following to the first line of the index function above:

Code:
$data['ppckeyword'] = $this->_ppckeywords($product);

Not sure if  I am on the right track here with the above, it was just a guess.



[/php]
Reply
#5

(This post was last modified: 01-19-2015, 07:55 PM by pdthinh.)

Quote:My landing page is actually at mysite.com/details/landingpage.

Can you explain how do you map url mysite.com/landingpage.php to mysite.com/details/landingpage?
Reply
#6

Quote:Can you explain how do you map url mysite.com/landingpage.php to mysite.com/details/landingpage?

In config>routes.php:

Code:
$route['details/(:any)'] = "details/index/$1";
Reply
#7

(This post was last modified: 01-20-2015, 06:16 PM by pdthinh.)

(01-20-2015, 07:21 AM)user2374 Wrote:
Quote:Can you explain how do you map url mysite.com/landingpage.php to mysite.com/details/landingpage?

In config>routes.php:

Code:
$route['details/(:any)'] = "details/index/$1";

If you follow CI URL (eg. mysite.com/segment1/segment2, the second segment is not the page you want to retrieve but the method name which handles your request. You can see the following example.

PHP Code:
class TestDetails extends CI_Controller
{
 
   public function index()
 
   {
 
       echo 'INDEX PAGE';
 
   }
 
   
    public 
function _remap($method$params)
 
   {
 
       echo '<b>Method :</b>';
 
       $this->_printHtml($method);
 
       // you can use $method to handle requested page
 
       // see http://www.codeigniter.com/user_guide/tutorial/static_pages.html
 
       
        echo 
'<b>Params :</b>';
 
       $this->_printHtml($params);
 
       
        echo 
'<b>$_GET :</b>';
 
       $this->_printHtml($_GET);
 
   }
 
   
    private 
function _printHtml($s)
 
   {
 
       echo '<pre>';
 
       var_dump($s);
 
       echo '</pre>';
 
   }


with URL http://mysite.com/testdetails/landingpag...d%20widget this code ouput
Method :

string(15) "landingpage.php"

Params :

array(0) {
}

$_GET :

array(1) {
 ["kw"]=>
 string(10) "red widget"
}
Reply
#8

My pages are already routed and functioning the way I want them, not sure if I gave you all the relevant code from my details controller:

PHP Code:
<?php

class Details1 extends CI_Controller {

 function 
__construct()
 {
 
parent::__construct();
 
 
$this->themes->set_theme('research');
 
$this->themes->set_template('landing1');
 
 }

 
 function 
index($product)
 {
 
$data['prodname'] = ucwords(str_replace('_'' '$product));
 
$data['keywords'] = $this->_keywords($product);
 
$data['descript'] = $this->_descriptions($product);
 
       $data['title'] = $this->_pagetitles($product);
 
$data['product'] = $product;
 
$data['view2'] = $this->_view_two($product);
 
//echo '<pre>'; print_r($data); echo '</pre>'; exit(); 
 
$this->themes->view('promote1/landingpages1'$data);
 
 } 


How would I implement the remap function with my current controller code above?
Reply
#9

Is this set to TRUE in your config?
Code:
$config['allow_get_array']        = TRUE;
Reply
#10

(01-19-2015, 01:31 AM)flemming Wrote: You could try using $this->input->get() rather thank $_GET

I'm not sure whether it will make any difference?



Code:
$kw = $this->input->get('kw');
echo $kw;

(01-21-2015, 02:25 PM)CroNiX Wrote: Is this set to TRUE in your config?


Code:
$config['allow_get_array'] = TRUE;


I don't seem to have that option in my config.php file. I am running CI version 2.0. I googled searched that line of code and found it in the CI version 2.2 docs, not sure if it's part of 2.0? Either way I added it to my config file but still not quite sure how to implement the code pdthinh posted, so that alone didn't help.

When using pdthinh's code, I am getting a blank screen with this:

Method :

string(5) "index"

Params :

array(1) {
 [0]=>
 string(19) "my_landing_page.php"
}

$_GET :

array(0) {
}



So were are at least getting somewhere. I just need to figure out how to use the remap function to work with my current controller setup (posted in my last post).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB