session - How do I filter sanitize input in PHP? -


i have code below i'm trying filter sanitize form requires in $_session because need items page added onto page , displayed cart list. anyway i'm having trouble getting code filter anything. doesn't work not sure i've done wrong here?[code][1]

if $_post data valid, customer details must added $_session , user forwarded new script called receipt.php

  <?php    session_start();  if(isset($_session['cart'])) {      $name =    filter_input(input_session, 'name', filter_sanitize_string);     $email =   filter_input(input_session, 'email', filter_sanitize_email);     $phone =   filter_input(input_session, 'phone', filter_sanitize_int);     $address = filter_input(input_session, 'address', filter_sanitize_string);    } ?> 

so @ point need submit form testing purposes, heres code @ current time:

session_start(); if(empty($_post['add']) === false ) {  $name =    filter_var($_post['name'], filter_sanitize_string); $email =   filter_var($_post['email'], filter_sanitize_email); $phone =   filter_var($_post['phone'], filter_sanitize_int); $address = filter_var($_post['address'], filter_sanitize_string);   } ?> <form method="post" action="cart.php" id="form1">      <label for="name">name:</label><br>     <input type="text" name="name">     <br><br>     <label for="email">email:</label><br>     <input type="email" name="email">     <br><br>     <label for="tel">phone:</label><br>     <input type="tel" name="phone">     <br><br>     <label for="textarea">address:</label><br>     <input type="textarea" name="address">     <br>      <div id="checkbox">         <input type="checkbox" value="remember">     </div>       <div id="label1">         <label for="checkbox">remember me</label>            </div>       <br>     <br>           <input type="submit" value="submit">     </form> 

i have debug module on page , in $_post array instead go when put html entities inside name input field still aren't being sanitised/removed.

debug module:

$_post contains: array ( [name] => frost [email] =>  [phone] =>  [address] =>  ) 

*frost shows in bold when add bold html tag input field whole point of stop happening.

input_session not exist - see http://php.net/manual/en/function.filter-input.php

in case, since looking store validated post data session code wrote wouldn't work if there input_session.

instead use this:

if (!empty($_post)       && ($name    = filter_input(input_post, "name", filter_sanitize_string))     && ($email   = filter_input(input_post, "email", filter_sanitize_email))     && ($phone   = filter_input(input_post, "phone", filter_sanitize_int))     && ($address = filter_input(input_post, "address", filter_sanitize_string))     ) {         $_session['foobar'] = [             'name'    => $name,             'email'   => $email,             'phone'   => $phone,             'address' => $address,         ]; } 

this way check post data has been submitted assertaining desired validity before assigning session array.

note assigning values inside if condition frowned upon, merely demonstrate type of approach required, ie, check , validate post before assigning session.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -