Avoid URL transform with PHP - php

I´m having issues with a link that is created in a php controller (Yii Framework). The link must be like this: https://example.com/track/?id=HDkuY0je9d (HDkuY0je9d is a tracking code) but when the view is rendered i get https://example.com/track/?id=h-dku-y0je9d
PHP is adding dash delimited automatically.
$trackcode = $val['tracking'];
$url_tracking = Yii::app()->createUrl("/track/?id=".$trackcode);
$tracking = 'Tracking';

If you are using Yii2 in this, it has helper class for create url and url must have been
$url_tracking = yii\helpers\Url::to(['/track','id'=>$trackcode]);
and for generate link
<?=\yii\helpers\Html::a('Link description',$url_tracking,['target'=>'_blank']) ?>

Thanks, I solved changing the way i get app url. Changed
$url_tracking = Yii::app()->createUrl("/track/?id=".$trackcode);
for
$url_tracking = websiteUrl()."/track/?id=".$trackcode;

Related

Codeigniter spaces in url

I have a method which returns all jobs by category.
The problem it's when i have spaces in the category name. How could I access those results?
for ex if i access http://localhost/management_system/Job/get_jobs_by_cat/Architecture it will return all the jobs from the Architecture category.
But when i try to access the category Information Technology I can't do it while I have spaces into the url, so I've tried with _ - and i didn't get any response.
How I can fix this issue?
You would likely want to use urldecode() to decode the url parameters in the controller function processing this logic. In your controller, do something like this:
$jobCat = urldecode($this->uri->segment(4));
Then, you would pass $jobCat to your model.
Here are some other Stack Overflow links that might help your cause.
How to pass parameters with space to controller from URL in
codeigniter?
Php - Codeigniter url spaces
what is the use of $this->uri->segment(3) in codeigniter
pagination
You need to URL encode Information Technology.
$category = 'Information Technology';
$encodedCategory = rawurlencode($category);
$url = 'http://localhost/management_system/Job/get_jobs_by_cat/' . $encodedCategory;
echo $url;
// http://localhost/management_system/Job/get_jobs_by_cat/Information%20Technology

How to create a hyperlink with parameters using DOM in PHP?

I am trying to create a hyperlink using DOM in this way:
//create a hyper link
$hyperlink = $dom->createElement('a',$info[0][id]);
$url = $dom->createAttribute('href');
$url->value="http:/mydomain.com/index.php?type=users&user_id=1";
$hyperlink->appendChild($url);
But the hyperlink doesn't work.
When I remove the parameters
?type=users&user_id=1
then it works fine.
How should I pass those parameters to the hyperlink properly?
I resolved this issue using #Perry's suggestion by adding the parameters to the url using CDATASection
$param = $dom->createCDATASection('?type=users&user_id=1');
$url->appendChild($param);

cakephp specify Theme for an Element in Controller

In cakephp, how do we specify which theme to use for an Element. I am initializing View object in Controller. I need to pass the element content as ajax response.
Controller :
$view = new View($this);
$view->layout = 'theme2';
$view->theme = 'newNav';
foreach($ctps as $ctpName)
{
$ctp[] = $view->element($ctpName);
}
At first I thought of accessing it as
$ctp[] = $view->element('../Themed/new/Elements/'.$ctpName);
But it obviously does not take care about element's directory. As some of the elements are in app/View/Elements and some are in app/ViewThemed/new/Elements/ directory.
Please suggest.
As per Arilia's suggestion, I am now trying
$this->viewClass = "Theme";
$this->viewPath = 'Elements';
$view = new View($this);
$view->theme = "new";
$view->layout = "theme";
$ctp = $view->element('userprofile');
echo json_encode($ctp);
die;
I am making an ajax call to this code. and it returns
"Element Not Found: Elements/userprofile.ctp"
what I was trying to develop was to render the basic page at first so that it can be cached. and then with the ajax call fetch the parts of UI that are session dependent and then replace the respective elements on browser.
To implement this, I tried fetching cakephp elements in controller, json encoding them and echoing them back. The code above though it worked for a normal dynamic page, it did not work for ajax calls. Cakephp somehow misbehaved here.
Now, as a better alternative, I have moved the code to View itself and I am echoing the json encoded from there. and as expected it is working well. Code is as
app/View/Themed/new/Elements/get_ctps.ctp :
<?php
foreach($ctps as $ctpName)
{
$ctp[] = $view->element($ctpName);
}
echo json_encode($ctp);
die;
?>
Its not a usual case. However, it might be helpful for someone.

How to pick up the database name and table name from the URL in PHP?

I am new to PHP.
I need a help regarding the methods of extracting DB name and table name from the given URL name.
For example, let's say, I have an URL like the one below:
/test.php?db=...&table=.../
How to extract the DB name and table name from this URL using PHP and use the result for other query purposes.
If you mean how to parse an existing URL for it's parameters:
parse_url() and parse_str() will help you strip the components of the url. You will primarily be looking at the following
$elements = parse_url($url);
$kvps = $elements->query;
$db = parse_str($kvps['db']);
$table = parse_str($kvps['table']);
But, if you mean how to GET variables from the current page before render:
<?php
$dbname = $_GET['db'];
$tablename = $_GET['table'];
?>
And yea, there are major security risks involved in opening up 'direct' access to your database this way. Best to obfuscate / encapsulate / wrap your functions in tasks like index.php&addUser=tim instead of index.php&insert=tim&db=boofar&table=users&dbuser=root&dbpassword=secure.
If you're just learning, what you're doing is fine, as long as you realize why it's wrong. If you're coding for production, you really need an alternate solution.
There are two ways to pass variables or data to another page.
GET (via the URL)
and
POST (usually a form submission)
You can alway get via
$_GET
http://php.net/manual/en/reserved.variables.get.php
or
$_POST
http://nl.php.net/manual/en/reserved.variables.post.php

How to create a short url for sharing on social media

I'm trying to write some code which shares a page on Facebook and twitter.
The problem I'm facing is that the page I'm trying to share has a big query string like:
http://domain.com/see.php?c=3&a=123&v=1
But it seems that Facebook and Twitter don't like that big query string.
I also tried using tiny url with following method in which I passed the URL to a PHP function to get the tiny URL:
var a = $("#Link").val();
I get the correct value of **a**. After that I pass this value to a PHP file:
$.post("ShortLink.php?value="+a
In that PHP file I got the following value:
http://domain.com/see.php?c=3
All the values after 3 is deleted.
Thanks
When POSTing to your ShortLink.php file, you should make sure to URL encode the value of a beforehand. Otherwise you're calling ShortLink.php?value=http://domain.com/see.php?c=3&a=123&v=1, i.e., sending:
value = http://domain.com/see.php?c=3
a = 123
v = 1
What you want is ShortLink.php?value=http%3A%2F%2Fdomain.com%2Fsee.php%3Fc%3D3%26a%3D123%26v%3D1, thus sending:
value = http://domain.com/see.php?c=3&a=123&v=1
This can be achieved via encodeURIComponent():
$.post("ShortLink.php?value=" + encodeURIComponent(a));
See also How do I pass a URL with multiple parameters into a URL? and How to encode a URL in Javascript?.
Why don't u just use an url shortener API for that, like Google url shortener. That way, you can leave your code the way it is, but for site's like Facebook and Twitter, it is nicely short.
Try this:
$.post("ShortLink.php?value=" + escape(a));

Categories