PDA

View Full Version : isset and spaces??


bphein1980
03-21-2006, 01:17 AM
I have this code:
<?
if(isset($_GET['g']))
{
$g = $_GET['g'];
$dirpath = "FULLPATH/galleries/$g";
echo "<h3>$g</h3>";
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh)))
{
if (!is_dir("$dirpath/$file"))
{
?>

<a href="javascript:popImage('login/galleries/<?php echo $g ?>/<?php echo $file ?>', '')">
<?php
echo "<img src='login/thumbs.php?im=galleries/$g/$file' />";
echo "</a>";
}
}
closedir($dh);
}
?>

I come to page like file.php?g=whatever. When "g" equals a name that has spaces, it only grabs the first word in the sequence....

So, if I used "file.php?g=what ever", when I use $g in the script, it will only come out to be "what" instead of "what ever".

Does that make sense? What am I not doing correctly?

It's strange, because the line with the javascript popImage when I view the source of the broken image... thats the problem... its only grabbing the first word.

But where the thumbs.php..... line is, the $g is grabbing the whole thing. Seems like is it used the same, but causing different results. I tried taking out the javascript and just linking directly to the image and it still does the same thing.

Any suggestions? Thanks!

Nightfire
03-21-2006, 01:35 AM
Should do more security checks from a variable that can be changed by joe bloggs.


<?php
$g = urlencode($_GET['g']);
if(isset($g))

Calilo
03-21-2006, 01:36 AM
Hi, Are you submiting the value for "g" from an html form? if no that might be the problem, remember you have to encode the string when you are going to submit it via _GET, that means no special chars, and white spaces become "+"

it should be

file.php?g=what+ever

that should make the trick

:thumbsup:
Calilo

bphein1980
03-21-2006, 01:58 AM
Thanks for the replies, I am still stuck on this.

What I am trying to do is populate a page with images in a folder. $g is the reference to the folder.

so when I do "index.php?g=image folder", I am telling the page to put "image folder" in the link and image source line.

It is very strange to me... the image source line works perfectly. When I hover over an image on the page, it shows the link correctly..."javascript: popImage('image folder/pic.jpg',' ')"

When I click the image, the pop up happens but displays a broken image which when I view the properties shows as "image", when it should be "image folder/pic.jpg". It basically stopping after the first space.

I really do not understand how this can be especially when hovering over the image shows the correct link.

If you need more code, let me know....
Here's the page that partially works. The TEST gallery works, but since the others have spaces, they dont work.
http://bigirongym.com/new/index.php?page=pictures

Thanks!

Calilo
03-21-2006, 02:16 AM
OK, mmm I think the problem is diferent, the problem is that when you create the popimage javascript, you have white spaces there, and apparently the javascript cant get executed that way, but if you change those to something else, i bet it will work...

my best suggestion would be not to use folders with white spaces, always use "_" folder_one, or similar, dont use white spaces, unix does not kike them.

Luck

not sure how and to what, you should change those white spaces, in Mac (Im a mac user) it is %20, not sure if it is the same for pc and all others.
Calilo

bphein1980
03-21-2006, 02:26 AM
Yes, it is definitely the whitespace that the javascript is not liking.

Anyone know how to replace the whitepage in "$g" with "%20".

Thanks for all the help.

Calilo
03-21-2006, 02:34 AM
There you go, just solved it...
$g = preg_replace("/ /", "%20", $g);

luck :thumbsup:
Calilo

bphein1980
03-21-2006, 02:44 AM
Thanks for the reply. That mod does actually work, just now that %20 is including in the image source, the browser renders it as "+" which breaks the path.

I appreciate all the input and help. I am going to head a different route. Im going to scrap the javascript.

Thanks for the help :)

Calilo
03-21-2006, 02:51 AM
Maybe "%20" is not what you are looking for, dont know what the correct one is for white spaces at urls, if you find it, just change if from my code, to what ever should be,

but anywat if tis better not to work with white spaces

Calilo

Nightfire
03-21-2006, 02:53 AM
Yes, it is definitely the whitespace that the javascript is not liking.

Anyone know how to replace the whitepage in "$g" with "%20".

Thanks for all the help.
See my first post:

$g = urlencode($_GET['g']);

or if that doesn't work:

$g = rawurlencode($_GET['g']);

bphein1980
03-21-2006, 04:18 AM
I know I havent been using PHP for very long, but this is the weirdest thing I have ever ran into.

Im using "$g = rawurlencode($_GET['g']);" which does take out the whitespace and shows it as "what%20ever".

When I click the image link, the popup still have a broken image, same problem... it is only take the first part.

This one might be a lost cause. Im going to scrap it and go a different route. Probably just enter the names as "what_ever".

Thanks for all the help. I appreciate it.

bustamelon
03-21-2006, 05:10 AM
Perhaps you need to escape the encoded strings for javascript?

\XXX XXX is an octal number (between 0 and 377) that represent the Latin-1 character equivalent. For example \251 is the octal code for the copyright symbol.
\xXX XX is a hexadecimal number (between 00 and FF) that represent the Latin-1 character equivalent. For example \xA9 is the hexadecimal code for the copyright symbol.
\uXXXX XXXX is a hexadecimal number (between 00 and FF) that represent the Unicode character equivalent. For example \u00A9 is the hexadecimal code for the copyright symbol.
(Note: Unicode is only supported by JavaScript 1.3)

http://html.megalink.com/programmer/jstut/jsTabChars.html
http://www.yuki-onna.co.uk/html/encode.html

So, something like:

<?
$g = rawurlencode($_GET['g']);
?>

<a href="javascript:popImage('login/galleries/' + document.write(escape("<?php echo "$g" ?>"))/<?php echo $file ?>', '')">
....
?>