简体   繁体   中英

Creating privacy settings for a users public profile

I'm trying to set personal privacy settings for users on their profiles by giving them the option to choose what info the public can see.

I'm testing out this sample query to try and figure this out.

Example:

The user submits their email, phone number, and an alternative phone number. All that info gets stored in the user database table.

I give the user the option to show that info by creating 3 new columns in the user table. The user then has the option to choose Yes or No if they want that info displayed to the public via a privacy form.

The new columns are called pe, pph, and paph. (The first p stands for private). pe equals email, pph equals phone, and paph equals alt-phone

By default that info is set to No, for non public viewing.

My question:

How can I associate

  1. pe to email
  2. pph to phone
  3. paph to alt_phone

Then run it in a query so if pe has a value of Yes, it appears in the output and if pph has a value of No, it does not appear in the output on the page.

I'm way out of my comfort zone on this one so please don't heckle me to bad, but here is what I have tried.

<?php
$id = (int)$_GET['id'];
$users = DB::getInstance()->query("SELECT `id`, `email`, `phone`, `alt_phone`, `pe`, `pph`, `paph`
FROM `users` WHERE `id` = $id AND `pe`='Yes' AND `pph`='No' AND `paph`='Yes'");

'pe' == ('email');
'pph' == ('phone');
'paph' == ('alt_phone');

foreach($users->results() as $u){
    ?>

<p align="center"><?php echo escape($u->email); ?></p>
<p align="center"><?php echo escape($u->phone); ?></p>
<p align="center"><?php echo escape($u->alt_phone); ?></p>
<?php } ?>

So the results should output: Users Email and the users Alternative Phone Number and not the users 1st phone number.

When the user changes their settings simply update the value of pe, ph and paph to either 0 or 1. 0 = keep it private whereas 1 = public.

Then the rest is simple:

    <?php

    $id = (int)$_GET['id'];
    $users = DB::getInstance()->query("SELECT `id`, `email`, `phone`, `alt_phone`, `pe`, `pph`, `paph` FROM `users` WHERE `id` = $id");

/*
Use this query if you make a new table:

$users = DB::getInstance()->query("SELECT `users`.`id`, `users`.`email`, `users`.`phone`, `users`.`alt_phone`, `users_settings`.`pe`, `users_settings`.`pph`, `users_settings`.`paph` FROM `users` LEFT JOIN `users_settings` ON `users_settings`.`userid` = `users`.`id` WHERE `users`.`id` = $id");

*/

    foreach($users->results() as $u){

?>

    <p align="center"><?php echo ($u->pe === false ? 'Hidden' : escape($u->email)); ?></p>
    <p align="center"><?php echo ($u->pph === false ? 'Hidden' : escape($u->phone)); ?></p>
    <p align="center"><?php echo ($u->paph === false ? 'Hidden' : escape($u->alt_phone)); ?></p>

    <?php } ?>

All I have done is use shorthand if queries to check if the setting for each value is 0 or 1. If it's 0 (ie false ) then don't show the number/address just say 'Hidden' instead. If 1 (ie true`) then display the variable collected from the query.

THE CODE IS UNTESTED AND MAY HAVE SMALL ERRORS/TYPOS. (and will, of course, only work if you use the pe, pph and paph fields as described above.

You should query the database for all the necessary fields (including your pe, pph etc.) when someone "views" the profile and have a conditional statement to display the details if the privacy field is set to "true" to make the details hidden. Don't echo the details out of set to private

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM