Jump to content

Personal user page with custom link


Dren
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi, i'm trying to create a function that generates a new user page for every users registered such as mydomain.tf/php?username=JhonDoe

I'm new to php and all the codes I tried to write just failed ...

Do you have any tutorials here or anyone who could help with simple codes?

 

Thanks!

Link to comment
Share on other sites

  • Solution

With dynamic content you dont create a new .php file for every user. You would have one .php file that generates the page with the requested users data from the database.

 

For example your go to mydomain.tf/user.php?username=JhonDoe and you should see the info about JhonDoe

 

Here is some example code for user.php

<?php
 
$con = mysql_connect("localhost","root","");
mysql_select_db('my_database');

// get username from url
if (isset($_GET['username']) && !empty($_GET['username']))
{
	// sanitize username
	$username    = mysql_real_escape_string($_GET['user']);

    // get users details form databse
	$sql = "SELECT username, name, website, email FROM members_table WHERE username='$username'";
	$result = mysql_query($sql,$con);

    // check that the query found a result
	if(mysql_num_rows() == 1)
	{
		// get the data from the query
		$user = mysql_fetch_assoc($result);

		// display users webpage
?>
<html>
<head>
	<title>User <?php echo $user['name'] ?></title>
</head>
<body>
	<h1><?php echo $user['username'] ?></h1>

	<p><b>Email:</b> <?php echo $user['email'] ?></p>
	<p><b>Website:</b> <?php echo $user['website'] ?></p>

</body>
</html>
<?php
	} // end if (line 15)
	// queyr did not find the username
	else
	{
		// display error
		echo $username .' could not be found';
	}
} // end if (line 7)
// someone didn't request for the username
else
{
	echo 'Please provide username';
}
?>
Link to comment
Share on other sites

 

With dynamic content you dont create a new .php file for every user. You would have one .php file that generates the page with the requested users data from the database.

 

For example your go to mydomain.tf/user.php?username=JhonDoe and you should see the info about JhonDoe

 

Here is some example code for user.php

<?php
 
$con = mysql_connect("localhost","root","");
mysql_select_db('my_database');

// get username from url
if (isset($_GET['username']) && !empty($_GET['username']))
{
	// sanitize username
	$username    = mysql_real_escape_string($_GET['user']);

    // get users details form databse
	$sql = "SELECT username, name, website, email FROM members_table WHERE username='$username'";
	$result = mysql_query($sql,$con);

    // check that the query found a result
	if(mysql_num_rows() == 1)
	{
		// get the data from the query
		$user = mysql_fetch_assoc($result);

		// display users webpage
?>
<html>
<head>
	<title>User <?php echo $user['name'] ?></title>
</head>
<body>
	<h1><?php echo $user['username'] ?></h1>

	<p><b>Email:</b> <?php echo $user['email'] ?></p>
	<p><b>Website:</b> <?php echo $user['website'] ?></p>

</body>
</html>
<?php
	} // end if (line 15)
	// queyr did not find the username
	else
	{
		// display error
		echo $username .' could not be found';
	}
} // end if (line 7)
// someone didn't request for the username
else
{
	echo 'Please provide username';
}
?>

 

Thank you! It works really fine, that was exactly what i was looking for :)

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.