Passing string and int in URL - php

I think this isnt a very typical question but i am stuck here.
I have to pass a String and an Integer in an URL for my PHP to process it.
I am building the link like...
$link = 'index.php?NAME=' . $name . '&id=' . $id;
but when it occurs in URL, its gets changed to
index.php?NAME=hello&id=10
and now id isnt being recognised by GET.

Use url_encode() for all of your variables. Or, better yet, use http_build_query().
$link = 'index.php?' . http_build_query(array(
'NAME' => $name,
'id' => $id
));
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.http-build-query.php

Link should work, can you elaborate how you're showing the link?

try this simple solution;
$link = 'index.php?NAME="{$name}&id={$id}"';

Related

Php string + XPath variable results in 0?

Iam new to xpath. I got a url using curl and domdocument but the problem is that the link is formated in this way: /bookstore/book.php
So then I wanna echo it to my own href link, it doesnot work ofcourse. The awnser would be to make a variable thats contains both the www.hello.com and the link I got from domdocument.
Here is my line of code:
$link = $linkquery->item(2)->nodeValue;
But if I do this it just gives me an 0
$url = "http://www.hello.com" + $link;
Any ideas? I guess I have missed something basic.
Regards
EDIT
Thanks for the help, the awnser was $url = "http://www.hello.com$link";
Isn't the string concatenation operator in PHP the dot operator .? So you want $url = "http://www.hello.com" . $link; or simply $url = "http://www.hello.com$link";.

Link not concatenating the way I imagine it to be

I created this variable to store this string:
$extension = '/index.php?Ah83kL80='.$id;
And I'm trying to add the $extension to this link:
<a href="'.Yii::app()->createUrl('image/index',array('album'=>$album->content)).'">
So far, I tried doing this:
<a href="'.Yii::app()->createUrl('image/index',array('album'=>$album->content, 'index' => $extension)).'">
But it put some sort of other characters that I never intended to include.
Output:
/index/%2Findex.php%3FAh83kL80%3D
According to the documentation I found, the second param is an array of URL parameters. That means that you don't want a string like '?Ah83kL80='.$id but want to pass the Ah83kL80 key and $id value separately, as you're already doing with album and $album->content.
I'm not familiar with Yii but try building your link like this... I'm going to format this differently than you have so I can indent and make it easier to read.
$link = Yii::app()->createUrl(
'image/index',
array(
'album'=>$album->content,
'Ah83kL80' => $id,
),
);

Request url on a save way

I would like to read a url. www.domain.com?cookie=set&redirect=yes
Now I want to use $ _SERVER['REQUEST_URI'] but this does not work with strip_tags and htmlspecialchars.
Also many I read that you should watch for XSS.
Does anyone know how to save a URL can be used by GET?
$url = "http://'.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI];
$url = strip_tags($url);//doesnt work
$url = htmlspecialchars($url);//doesnt work
Thanks!
Edit to (doesnt work):
$url = "http://".$_SERVER[HTTP_HOST]."".$_SERVER[REQUEST_URI];
$url = strip_tags($url);
echo $url;
for example
www.domain.com?cookie=set&redirect=yes
output => index.php?cookie=se%3Cscript%3Et&re%3Cb%3Ed%3C/b%3Eirect=yes
This line
$url = "http://'.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI];
Needs to be either
$url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
or
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
The way you are currently doing it will not concatenate the data correctly.
Issues with the your line:
Your mixing quotes around the protocol " to open and ' to close
You are not quoting the $_SERVER params e.g $_SERVER['PARAM']
You are not joining the 2 $_SERVER vars with anything so you'll get a syntax error

Clean way to write urls instead of concatenation

I have been finding myself doing URLs like this:
$link = base_url('post') . '/' . $post_id . '/' . $slug . '/page/' . $page_num;
To form http://example.com/post/10/some-post-name/page/1
Needless to say, it's pretty messy, but I can't think of an alternative? Is there a better way write links with variables in it?
I am using Codeigniter as a framework if there is a solution involving it.
You have a few ways:
First, via sprintf:
sprintf('%s/%s/%s/page/%s', base_url('post'), $post_id, $slug, $page_num);
Or via an array implode:
implode('/', array(base_url('post'), $post_id, $slug, 'page', $page_num));
Or if you put all your values into variables, you can take advantage of string interpolation.
$url = ...;
...
"$url/$post_id/$slug/page/$page_num";
The last one is longer when you take into account the variable assignment block, but it combines succintness with readability.
Use sprintf:
$link = sprintf('%s/%d/%s/page/%d', base_url('post'), $post_id, $slug, $page_num);
You could do something like this:
$link = site_url("post/{$post_id}/{$slug}/page/{$page_num}");
You really should be using site_url() instead of base_url() for CI links. base_url() is meant for non-CI assets, like images and css.
site_url() will point to the correct front controller path, so you can update your configuration at will, and everything using that to build paths will update accordingly.
I revised my answer. Use the curly brace notation and avoid using extra functions. You can pass an array of arguments to the function, like so:
$link = site_url(array('post', $post_id, $slug, 'page', $page_num));
But working with arrays is slower. This can be useful if you need to dynamically build the url, though.
You could do it the old fashioned way with a function!
function buildlink($base_url,$post_id,$slug,$page_num)
{
return $base_url . '/' . $post_id . '/' . $slug . '/page/' . $page_num;
}
call it like this
$link = buildlink(base_url('post') ,$post_id, $slug ,$page_num);
but maybe i'm missing something

Constructing a URL based on existing variables

I'm a bit of a PHP newbie, and I'm trying to do what I believe is quite a complicated operation on a website. So I need your help :)
What I want to do is request a specific page for each client, the only URL variables involved are at the end, as normal.
Basically my variables have been set previously in the script, what I want to say is;
The URL is http://site.com/index.php?image=$clientnumber Somehow.
if you guys could give me some insight on how to do this, that would be great!
You can use the sprintf function to do this:
$url = sprintf('http://site.com/index.php?image=%s', $clientnumber);
PHP String Operators
$new_url = "http://site.com/index.php?image=$clientnumber";
or
$new_url = 'http://site.com/index.php?image=' . $clientnumber;
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
Results in:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
For your needs:
$data = array('image'=> $clientnumber);
echo 'index.php?' . $data;
Will give you:
index.php?image=878787283

Categories