How to make Page visible only for friends

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 125
Location: Holland Rotterdam

How to make Page visible only for friends

How can i create that this code is only visible for friends-list ?

Code: Select all

<?php
include('connection.php');

$id = (int) $_GET['id'];

$sql=mysqli_query($conn,"SELECT * FROM `user` WHERE id='" . $id . "' ");
$users=mysqli_fetch_assoc($sql);
?>

<?php echo $users['user_relationship'];?>

Admin
Hello
It depends of the structure and relationship of your tables.
Generally, you should make something like this:

Code: Select all

<?php
//get the ID of the owner-user of accessed page
$id_user_pg =2;

//get and store in Session an array with the IDs of the friends of current user ($id)
$id_friends =[1, 2, 8];

//if $id_user_pg is in $id_friends shows the page, else some message
if(in_array($id_user_pg, $id_friends)) echo 'page';
else echo 'Not allowed to see the content';
?>

JanMolendijk
Ok i tried some things with the code but did not work out.

I have two tables one table with profile information, and a friend system for this i have also a table...

member_access

Code: Select all

id context   user_id     friend_id   friend_name   
user

Code: Select all

id  name  email  pass  mobile  gender 
I want table user only be visible for the friend_id from member_access from eatch user_id

Admin
An important thing is to get the ID of the owner-user of the accessed page.
Then, you make a Select to check if that ID is friend with the user who accesses the page.

Code: Select all

//id of current user
$id =4;

//here you should get the ID of the owner-user of accessed page
$id_user_pg =2;

//check if current user ($id) is ftiend with $id_user_pg
$sql=mysqli_query($conn,"SELECT friend_id FROM member_access WHERE friend_id=$id_user_pg AND user_id=$id LIMIT 1");
$nr_rows = mysqli_num_rows($sql);

//if $id_user_pg is friend with $id shows the page, else some message
if($nr_rows >0) echo 'page';
else echo 'Not allowed to see the content';

JanMolendijk
This where realy helpfull thanks for your support

Similar Topics