Jump to content

User Profiles and Following Users


ibz786

Recommended Posts

Hi, for my coursework i need to make a microblogging website, similar to that of Twitter

 

I have created a MySQL Database, and also the login and registration forms

I am able to log in as a user and also change user submitted information i.e. Name, About Me etc

 

The problem i am having is that when i access the logged in users profile page e.g. John Doe, i can access it fine

However, when i wish to access say James Smith or any other user, i am redirected back to John Doe's profile

 

login.php

$query = mysql_query("SELECT * FROM users WHERE username = '$user'") or die(mysql_error());
      $login = mysql_fetch_array($query);
      
      if(md5($pass) == $login['password'])
      {
         $_SESSION['user'] = $login['id'];
         header("Location: home.php");   
      }

 

home.php

<?php
include ('includes/connect.php');
session_start();

$query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['user'] . "");
$user = mysql_fetch_assoc($query)

?>


<a href="profile.php?id=<?php echo $user['id']; ?>"> Profile | </a>

 

users.php

<?php
include ('includes/connect.php');
session_start();

$query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['user'] . "");
$user = mysql_fetch_assoc($query)

?>

<?php
$members = mysql_query("SELECT * FROM users");
while($allusers = mysql_fetch_assoc($members))
{
   echo
   "<table>
    <tr>
    <td>
    <a href=\"profile.php?id=" . $allusers['id'] . "\">" . $allusers['fullname'] . "</a>     </td>
    </tr>
    </table>";   
}

?>

 

In all honesty i do know that the fault lies with the $_SESSION bit of the code since everything i do will only access the profile of the user who is logged in

 

However i dont know how i am able to allow the logged in user to access their own profile as well as view other people's profile.

 

 

 

Another problem is how do i follow users?

I have a table called 'follow'

I have two columns, user_id and follower_id, both being foreign keys

 

However i honestly dont understand how to use PHP code to make users follow each other

 

If anyone could assist me with any part of this i would be very grateful

 

 

Thank You

Link to comment
Share on other sites

How are you gathering what page to view on users.php? I don't see anything relating to a query string or anything. Try something like:

if (isset($_GET['user'])) {
$user = $_GET['user'];
} else if (isset($_SESSION['user'])) {
$user = $_SESSION['user'];
}

if (isset($user)) {
$user = mysql_real_escape_string($user);

$query = mysql_query("SELECT * FROM users WHERE id='" . $user . "'");

if (mysql_num_rows($query)) {
	// user found, do whatever
} else {
	echo 'user not found';
}
} else {
echo 'you must be logged in to view your profile';
}

 

To view the profile you'd go to users.php?name=James Smith If you don't supply a name, it tries to use the session. If it can't find the session either, it will say you need to login to view your profile.

 

To "follow" other users, basically you're just going to fetch all posts by that user. So say your follow table is like:

user_id | follow_user_id
-------------------------
1       | 2
1       | 3
1       | 4

 

So user 1 (lets say that's you) is following users 2, 3 and 4. Then you would just select the posts these users make in whatever fashion you wanted.

Link to comment
Share on other sites

Hey, thanks for that i shall try it. Just to quickly ask, the code you have posted do i put that in profile.php?

My apologies for the confusion

 

Ok heres what i mean:

 

in home.php the page is operating under the session of the currently logged in user, e.g. id = 1 is John Doe for example

using the code i have for home.php i can access John Doe's profile since his id matches that of the users database

 

I created users.php to display all the users as links, so if i wish to view their profile all i need to do is click on them

when i hover over the user names, their id value appears

e.g. profile.php?id=3  profile.php?id=4 etc

 

However due to an error in profile.php i can only access the logged in users profile

 

My apologies if i repeating myself or not making sense i haven't had much sleep lol

 

Link to comment
Share on other sites

I created users.php to display all the users as links, so if i wish to view their profile all i need to do is click on them

when i hover over the user names, their id value appears

e.g. profile.php?id=3  profile.php?id=4 etc

 

Okay, I misunderstood what users.php was for. So then yes, this would be for profile.php

Link to comment
Share on other sites

Ok to the profile.php file i have added this

 

<?php
include('includes/connect.php');
session_start();



if ($_GET['user'] != "")
{
$query = mysql_query("SELECT * FROM users WHERE id = " . $_GET['user'] . "");
} else
{
$query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['user'] . "");
}
$user = mysql_fetch_assoc($query)

?>

 

The $_GET bit doesnt work

 

I understand that the $_SESSION['user'] shall allow for me to get the logged in users profile

 

But i I dont know how to grab other users profile using the $_GET method, if someone could help i would be grateful

 

Thank You

Link to comment
Share on other sites

this is:

 

users.php

<?php
include ('includes/connect.php');
session_start();

$query = mysql_query("SELECT * FROM users WHERE id = " . $_SESSION['user'] . "");
$user = mysql_fetch_assoc($query)

?>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<div id="banner" style="background-color:#000000">
<img src="images/logo.png">

<form id="searchbox" action="#" method="post">
<input type="text" id="search" name="search" value="">
<input type="submit" id="submit" name="submit" value="Search">
</form>


<a href="home.php"> Home | </a>
<a href="profile.php?id=<?php echo $user['id']; ?>"> Profile | </a>
<a href="#"> Messages | </a>
<a href="settings.php"> Settings | </a>
<a href="users.php"> Users | </a>
<a href="logout.php"> Sign Out </a>

</div>

<div>

<?php
$members = mysql_query("SELECT * FROM users");
while($allusers = mysql_fetch_assoc($members))
{
echo
"<table>
 <tr>
 <td>
 <a href=\"profile.php?id=" . $allusers['id'] . "\">" . $allusers['fullname'] . "</a>     </td>
 </tr>
 </table>";	
}

?>
</div>

</body>

</html>

 

So basically what im trying to do here is when i hover the usernames which are links

they say:

localhost/sblog/profile.php?id=1 or 2 or 3 depending on their links etc

So when i click on the links i want them to take me to the users profile which i have clicked on and not my own

 

Hope that makes sense

Link to comment
Share on other sites

This is how I would combat this problem:

<?PHP session_start();

  include('includes/connect.php');

  //### Get the profile "id" if it exists
  $GET_ID = isSet($_GET['id']) ? intVal($_GET['id']) : 0 ;

  //### If $GET_ID does not equal 0 we are safe to search the accounts
  if($GET_ID != 0) {

    //### Check to see if we can find an account matching the ID given
    $checkUser = mysql_query("SELECT * FROM `users` WHERE `id` = {$GET_ID}");

    //### If the account exists, we assign the query result to user
    //### If the account does not exist, we query and fetch the logged in user's detail or redirect with an error?
    if(mysql_num_rows($checkUser)) {
      $user = mysql_fetch_assoc($checkUser);
    } else {

      //### Fetch current logged in users details
      $loggedUser = mysql_query("SELECT * FROM `users` WHERE `id` = {$_SESSION['user']}");
      $user       = mysql_fetch_assoc($loggedUser);

      //### OR re-direct to the users profile?
      //header('Location: profile.php'); exit;
    }
  } else {
    //### Fetch current logged in users details
    $loggedUser = mysql_query("SELECT * FROM `users` WHERE `id` = {$_SESSION['user']}");
    $user       = mysql_fetch_assoc($loggedUser);
  }

  echo '<pre>';
  print_r($user);
  echo '</pre>';

?>

 

Try this out and tell me how it goes :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

In the words of Doc Brown from, Back to the Future "Great Scott!!" :D

 

I think that code has fixed it

I am very grateful for yours and everyone's help, very much appreciated

 

Thank You very much :)

Just a question, how did you figure it out? I ask since i wish to better my PHP coding skills

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.