• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

PHP Question(s)

Msap14

New Member
Joined
Nov 11, 2008
Messages
156 (0.03/day)
Location
US
System Name Piece
Processor Ahtlon 2 X2 3GHz
Motherboard AsRock
Cooling Big sink and fan
Memory 4GB
Video Card(s) 9600
Storage three , don't remember what they are
Display(s) Asus 22" wide
Case Xigmatek
Power Supply a good one
hey all,

i am teaching myself a bit of PHP and have a question to ask...

the book i have shows the following:

function sanitizeString($var)
{
$var = strip_tags($var)
$var = htmlentities($var);
$var = stripslashes($var);
return mysql_real_escape_string($var);
}

what is the importance of all the $var variables? :confused:
does this stack values on a single variable?
why/how do you start naming a variable $var as strip_tags with another $var in parenthesis?

the book doesn't say, it is a rather simple book, just teaching the basics and this is part of an exercise.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.62/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
$var contains the input.

For example, if $var was equal to "Hello world!<br/>\\a>b\\r\\nYay!":

After $var = strip_tags($var), $var would equal "Hello world!\\a>b\\r\\nYay!"
After $var = htmlentities($var), $var would equal "Hello world!\\a&gt;b\\r\\nYay!"
After $var = stripslashes($var), $var would equal "Hello world!\a&gt;b\r\nYay!"
After $var = mysql_real_escape_string($var), $var would equal "Hello world!\\a&gt;b\\r\\nYay!"

Effectively, it is making whatever is sent through it injection-proof.
 
Last edited:

Msap14

New Member
Joined
Nov 11, 2008
Messages
156 (0.03/day)
Location
US
System Name Piece
Processor Ahtlon 2 X2 3GHz
Motherboard AsRock
Cooling Big sink and fan
Memory 4GB
Video Card(s) 9600
Storage three , don't remember what they are
Display(s) Asus 22" wide
Case Xigmatek
Power Supply a good one
makes sense since the function is named sanitizeString.

Now i'm wondering why you would need 4 different variations?
could it be just to show the different ways you could use the variable?
i think all the variations are used throughout the entire example though.

ill probably have more questions as i read through this book, so i apreciate the help so far.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.62/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Those four functions don't modify $var, they make a copy of it and return the modified input. Think of it like this:
$ReturnvedValue = function($InputValue)

By reusing $var, you're applying the changes to $var over and over. By the way, this would have the exact same output as the code you pasted:
PHP:
function sanitizeString($var) { return mysql_real_escape_string(stripslashes(htmlentities(strip_tags($var)))); }
 
Joined
Oct 10, 2008
Messages
3,471 (0.61/day)
System Name Acer Aspire V3-771G-53218G75Maii
Processor Core i5 3210M (2,5-3,1Ghz)
Memory 8GB DDR3 SODIMM
Video Card(s) Geforce GT650M
Storage Samsung 830 256GB - 750GB Toshiba drive
Software Windows 7 x64 Home Premium (non-acer-bloatware)
makes sense since the function is named sanitizeString.

Now i'm wondering why you would need 4 different variations?
could it be just to show the different ways you could use the variable?
i think all the variations are used throughout the entire example though.

ill probably have more questions as i read through this book, so i apreciate the help so far.

This function is used to strip several types of special values from an inputted string. This is used to counteract stuff like SQL injection, and makes sure that what you have left after you put your string through this function is just text.

What it does (very basically):
- Takes your input string, and stores it in the $var variable.
- calls the strip_tags() function, which well, strips tags from a text string, and stores the result in $var
(so $var now contains your input, minus any tags that were just stripped)
- calls the htmlentities() function, which filters out any HTML-specific code someone might have entered in your input, and stores the result in $var
(so now $var has been cleaned in 2 different ways already!)
- same for the stripslashes() function
- and last, but not least, the SQL stripper

so it does not do 4 variations of the same, it sequentially processes all these four commands, each time updating the $var variable with the filtered result.
 

Msap14

New Member
Joined
Nov 11, 2008
Messages
156 (0.03/day)
Location
US
System Name Piece
Processor Ahtlon 2 X2 3GHz
Motherboard AsRock
Cooling Big sink and fan
Memory 4GB
Video Card(s) 9600
Storage three , don't remember what they are
Display(s) Asus 22" wide
Case Xigmatek
Power Supply a good one
whats the best way to go about styling a php website?

should the style code go inside the the php or vise versa?
how can i link a style sheet? (normal html linking doesn't seem to be working)
 
Last edited:
Joined
Oct 10, 2008
Messages
3,471 (0.61/day)
System Name Acer Aspire V3-771G-53218G75Maii
Processor Core i5 3210M (2,5-3,1Ghz)
Memory 8GB DDR3 SODIMM
Video Card(s) Geforce GT650M
Storage Samsung 830 256GB - 750GB Toshiba drive
Software Windows 7 x64 Home Premium (non-acer-bloatware)
CSS, all the way :)
try using the include function
 

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
Within the head tag of your html or php page:

Code:
<link rel="stylesheet" href="style.css" />
 
Top