ibz786 Posted January 3, 2012 Share Posted January 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/ Share on other sites More sharing options...
scootstah Posted January 3, 2012 Share Posted January 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1303875 Share on other sites More sharing options...
ibz786 Posted January 3, 2012 Author Share Posted January 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1303881 Share on other sites More sharing options...
scootstah Posted January 3, 2012 Share Posted January 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1303885 Share on other sites More sharing options...
ibz786 Posted January 3, 2012 Author Share Posted January 3, 2012 Erm unfortunately that code didnt work In addition i dont understand it, hmm im rather stuck at the moment :/ Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1303893 Share on other sites More sharing options...
PaulRyan Posted January 3, 2012 Share Posted January 3, 2012 Do you currently have a profile.php file? If so post it so we can see your current code, if not then create something you think will work, if it doesn't then we'll gladly help. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1303900 Share on other sites More sharing options...
ibz786 Posted January 4, 2012 Author Share Posted January 4, 2012 hi sorry i passed last night lol was rather tired This is my profile.php code 17261_.php Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304177 Share on other sites More sharing options...
ibz786 Posted January 4, 2012 Author Share Posted January 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304193 Share on other sites More sharing options...
scootstah Posted January 4, 2012 Share Posted January 4, 2012 What is the URL you are using to try and get the profile? Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304231 Share on other sites More sharing options...
ibz786 Posted January 4, 2012 Author Share Posted January 4, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304243 Share on other sites More sharing options...
PaulRyan Posted January 4, 2012 Share Posted January 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304253 Share on other sites More sharing options...
ibz786 Posted January 5, 2012 Author Share Posted January 5, 2012 In the words of Doc Brown from, Back to the Future "Great Scott!!" 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 Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304359 Share on other sites More sharing options...
PaulRyan Posted January 5, 2012 Share Posted January 5, 2012 Such a great quote Not a problem, glad I could help. I just thought about the situation logically, and then coded to follow the logic (plus I've done similar to this 100 times before at least ). Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/254296-user-profiles-and-following-users/#findComment-1304377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.