user3054322
user3054322

Reputation: 1

Making profile page unique by adding unique ID in URL. But how?

I'm creating a social network script using PHP/MySQL. Everything is working fine. Homepage,Profile, edit profile etc. But the Profile page has URL - http://www.localhost/socialnetwork/profile.php.

I have 5 dummy users and When profile is accessed using their usernames/password. The same URL can be seen on browser.

But I want something like -

URL - http://www.localhost/socialnetwork/profile.php?u=testuser.

Where 'u' can be a unique user Id in database. How to implement it ? Here's my code -

<?php  
include("includes/session.php");
include("includes/database.php");
include("includes/user.php");
include("includes/functions.php");
if(!$session->is_logged_in()) { redirect_to("login.php");}
?>

 <?php
$user_id = $_SESSION['id'];
// - Here's What I have added  - But nothing changes. The URL is same.
if(isset($_GET['id'])){
$uid =   $_GET['id']; 

}

$sql    = "SELECT * FROM users WHERE id = '$user_id' LIMIT 1"; 
$result = $database->query($sql);
while($record = $database->fetch_array($result)){

$fname      = $record['fname'];
$lname      = $record['lname'];
$email      = $record['email'];
$sex        = $record['sex'];
$password   = $record['password'];
$phone      = $record['phone'];
$birthday   = $record['birthday'];
$display    = $record['dp'];
$bio        = $record['bio'];
 }

Upvotes: 0

Views: 2311

Answers (3)

edwin cooper
edwin cooper

Reputation: 1

add this in your while loop

$id=$record['id'];

then make a link in your while loop like this

echo '<p>
<a href="the url of your profile page.php?id='$id'">

'.$fname.'

    </a></p>';

this will echo out all the names in your db,and add uniqueness to them. And when your on your profile page you can get the id of that particular user using

echo $_GET[id'];

Upvotes: 0

Alberto Fontana
Alberto Fontana

Reputation: 948

BEFORE PROFILE.PHP

When the user "foo" (a dummy username) logins, he is redirected to the page

http://www.localhost/socialnetwork/profile.php?u=foo

Great! But how do we do it? The redirection is made in PHP, with something like:

header('location: /socialnetwork/profile.php?u=' . $theusername_just_logged);

The header() function basically redirects to another page. After that function is called, you will land into the page

http://www.localhost/socialnetwork/profile.php?u=foo

INTO PROFILE.PHP

Inside the file profile.php, then, you retrieve the username through the variable $_GET. In this particular case, $_GET['u'] will contain the username, which is "foo". Here more info about $_GET

Once you retrieved the username, you can query the database to get all the infos of that user and build the page basing on them!

This way, basing on the $_GET['u'] you will always get a different page! Please consider to make a proper error page if the username doesn't exists in the database

Hope it helped

Upvotes: 0

Peter van der Wal
Peter van der Wal

Reputation: 11856

  1. Use $_GET['u'] to read out the url
  2. Have in your DbTable a column username whith a UNIQUE INDEX
  3. Use a SQL-query combining both to read the user-profile

Upvotes: 1

Related Questions