Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Credit Card Number - Convert to *****4123 1

Status
Not open for further replies.

sipps

Technical User
Feb 9, 2003
133
GB
Hi,

I would like to show the customers credit card number on previous bookings that they have had, but only the last 4 digits of the card number, does anyone know how to do this, and make all the other numbers ****'s?

Thanks
 
split the number into about 4 sections- and store them in non logical names to make it harder for hackers to find(you should be doing this if you are storing the number on your web server anyway- and if you don't own the server, I suggest you think twice about it)
then when you retreive the number use something like substr_replace() to replace the output to the browser with ***
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
Or...

Code:
<?php

$masked = str_repeat(&quot;*&quot;, strlen($cc) - 4);
$last_four = substr($cc, -4, 4);

echo $masked.$last_four;
?>

-Rob
 
Thanks Rob,

How would I integrate that into my page though? The query that gets the credit card number from the db, amongst other things is:

mysql_select_db($database_fmcrentals, $fmcrentals);
$query_getbookingdetails = sprintf(&quot;SELECT * FROM booking, customer WHERE Booking_ID = '$HTTP_SESSION_VARS[Booking_ID]' AND booking.Customer_ID = customer.Customer_ID AND Username = '$HTTP_SESSION_VARS[MM_Username]'&quot;, $colname_getbookingdetails);
$getbookingdetails = mysql_query($query_getbookingdetails, $fmcrentals) or die(mysql_error());

And then I call the Credit_Card_Number by:

<?php echo $row_getbookingdetails['Credit_Card_Number']; ?>

I would like to integrate this script so it only shows the last four digits. Will it matter if the card number length varies?

Thanks
 
So, by assuming what's going on... I would do something like this...

replace $cc in my example with

$row_getbookingdetails['Credit_Card_Number']

everywhere it shows up, then replace my echo statement with

$row_getbookingdetails['Masked_Credit_Card_Number'] = $masked.$last_four;

then whenever you do an echo, echo that variable instead.

And as long as the number has more than 4 digits it'll figure out the rest because it's using strlen() of the number, and just taking four less than that as the number of *'s to show... then it uses substr() to get the last 4 numbers.

substr(<which string>, <where to start>, <how many to get>)

So we're starting at -4, which is 4 from the end, and getting 4.

Hope that's useful.

-Rob
 
Thanks so much Rob, worked a treat!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top