Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-08-2012, 07:39 PM   PM User | #1
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Problem with Photo Labels

I am having trouble with my Photo Labels getting messed up by htmlentities().

On my website, I have this code...

PHP Code:
title='" . str2htmlentities($photoLabel) . "' /> 
...so that if you hover over a Member's Photo you can see an additional caption like this...

Quote:
Debbie's brand new car!!

The problem is that htmlentities() is changing things to this...

Quote:
Debbie & #039 ; s brand new car!!
(I've added spaces so it doesn't converted by this website.)


Obviously I can't control when a Member *legitimately* wants to add something like an Apostrophe to their Photo Label, and it looks broken - at best - to see all that unicode or whatever in the display?!


Is there a way to protect against XSS attacks and yet not muck up the Photo Labels??

Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 07-08-2012, 08:05 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by doubledee View Post
Is there a way to protect against XSS attacks and yet not muck up the Photo Labels??
Use strip_tags() which will remove any html / javascript tags and don't use htmlentities() on the mouseover.

htmlentities() is used for displaying characters in a webpage that would otherwise be understood as html source code by the browser. Your mouseover box that the browser displays is not part of a webpage but a windows control - part of the windows control set available to all programs. Despite being on a mac, I suspect apple use a similar method, especially since Bill once had dealings with them. The mouseover box control therefore works differently and doesn't need any htmlentities() use because its just a normal display component not a TWebBrowser VCL.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-08-2012, 08:52 PM   PM User | #3
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by tangoforce View Post
Use strip_tags() which will remove any html / javascript tags and don't use htmlentities() on the mouseover.

htmlentities() is used for displaying characters in a webpage that would otherwise be understood as html source code by the browser. Your mouseover box that the browser displays is not part of a webpage but a windows control - part of the windows control set available to all programs. Despite being on a mac, I suspect apple use a similar method, especially since Bill once had dealings with them. The mouseover box control therefore works differently and doesn't need any htmlentities() use because its just a normal display component not a TWebBrowser VCL.
Compelling response, but it chops off everything after the apostrophe?!

From...

Quote:
Debbie's New Car

To...
Quote:
Debbie


Debbie
doubledee is offline   Reply With Quote
Old 07-08-2012, 09:02 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Thats probably going to need escaping in the output then.

Debbie's brand new car!!

would become

Debbie\'s brand new car!!

Just as you would escape an apostrophe in php, you sometimes need to do it in javascript and in html titles so that the browser can parse the source correctly. You can use addslashes() for that before printing the title into the page.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-08-2012, 09:12 PM   PM User | #5
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
After looking at things a little closer, I have found some really STRANGE behavior I cannot explain...

If I go into the Member's Profile page, there is a photo of the logged in Member, plus a listing of the Member's Friends.

If I hover over any of those photos, I see things like this...

Quote:
Pam's Red Gradient
Quote:
Sam's Spiral GIF

And here is a code snippet for "profile.php"

PHP Code:
    "<img src='/uploads/"
        
validatePhoto($topPhoto$topPhotoApproved) .
        
"' width='60' alt='Thumbnail of "
        
$topUsername .
        
"' title='"
        
str2htmlentities($topPhotoLabel) .
    
"' /> 


Now, if I go into one of the Article pages, at the bottom is a series of Member Comments, and next to each is the Member's Photo.

If I hover over any of those photos, I see things like this...

Quote:
Pam & #039 ;s Red Gradient
Quote:
Sam & #039 ;s Spiral GIF

And here is a code snippet for "article.php"

PHP Code:
    <img class='noborder' src='/uploads/"
    . validatePhoto($photoName, $photoApproved) .
    "' 
width='100'
    
alt='Photo of " . $username . "'
    
title='" . str2htmlentities($photoLabel) . "' /> 

While the variable names are slightly different, all of these Photos and Photo Labels are coming from the *same* Fields and Records in the Database, so they should look identical across pages unless my code was different, which is does not appear to be?!


Any idea why things seem to be working on my Profile Page, but are broken on my Article/Member Comments Page??

Thanks,


Debbie
doubledee is offline   Reply With Quote
Old 07-08-2012, 09:23 PM   PM User | #6
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Looks like this line in "article.php" was causing the confusion...

PHP Code:
    // Set Photo Label.
    
$photoLabel = (!empty($photoLabel) ? str2htmlentities($photoLabel) : str2htmlentities($username)); 
Oops!!!


Debbie
doubledee is offline   Reply With Quote
Old 07-08-2012, 09:31 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Glad you got it working

Don't suppose you feel like fixing my edimax routers port forwarding?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-08-2012, 09:46 PM   PM User | #8
doubledee
Regular Coder

 
doubledee's Avatar
 
Join Date: Mar 2011
Location: Arizona
Posts: 617
Thanks: 19
Thanked 0 Times in 0 Posts
doubledee has a little shameless behaviour in the past
Quote:
Originally Posted by tangoforce View Post
Glad you got it working

Don't suppose you feel like fixing my edimax routers port forwarding?
If I knew what those were, I might?!


Debbie
doubledee is offline   Reply With Quote
Old 07-09-2012, 01:46 AM   PM User | #9
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by doubledee View Post
If I knew what those were, I might?!
A PITA. I've been struggling to get port forwarding working properly for use with a SMTP server I've been working on. I think I'll go with a linksys or D-Link router in the future.. Not impressed with Edimax.

Still, got there eventually.. and I can add some more features to my SMTP server I can now send emails to php scripts where they can be processed instantly (no cron or piping needed)
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-09-2012, 01:47 PM   PM User | #10
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
Originally Posted by tangoforce View Post
. Not impressed with Edimax.
yup, buying an edimax is a crime which ironically is also the punishment
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 07-09-2012, 02:03 PM   PM User | #11
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Lol, so what would you recommend as a solid router?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 07-09-2012, 04:55 PM   PM User | #12
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
linksys/cisco is on the ball, and netgear, for budget brands TP-Link stuff is actually much better than it ought to be, I am not a fan of d-link but many are.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 07-09-2012, 05:03 PM   PM User | #13
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Hmm, I had a wifi adapter for a laptop years ago made by netgear but sold under a budget name, it was crap (by that I mean it simply didn't work - could barely scan and never connect). Went and got a D-Link and it worked straight out of the box with minimal fuss so I was pretty impressed with that. I've never had any trouble with it either although that laptop is now dead and I use a netbook so it's redundant and these days they all have it built in..

I have an old cisco router thing sat in the shed that I've never used.. came from someone on freecycle. It's big, long, flat.. I suppose I should look at it one day and see what it actually does. I remember looking at it a while back and thinking it looked a lot more complex than a normal router.. Maybe I should look for a linksys..
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 AM.


Advertisement
Log in to turn off these ads.