Jump to content

Help with my project


seadog

Recommended Posts

so im workin on a project and the base code i need to use is PHP. now i dont know much about php and i have a question. My project is quiet similar to myspace but much much much much smaller. I want the users to be able to register, set up a profile, then have their profile set up in the URL.  ex. website.com/usersprofile ex2. website.com/myprofileusername

Im having no troubles yet with inputting data. displaying it im still learning. but im baffled as to how once the user sets up their profile it can be viewed by URL. can anyone help me out with this system?

Link to comment
Share on other sites

u need to use sessions on the login, there make dynamic links using there sessions.

here is an example

<?php
require_once 'db_connect.php';
require_once 'nav_bar.php';
require_once 'logged_in.php';

if ($_SESSION['is_valid'] == true){

$user_id = $_SESSION['user_id'];
$sql = "SELECT * FROM $user WHERE `user_id`='{$user_id}' LIMIT 0,1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$username = $row['user_name'];
$email = $row['user_email'];
echo "$username<br>";
$show_email = $row['show_email'];
if ($show_email == 1)
{ 
echo "Email:<a href='mailto:$email'>$email</a>";

}
elseif ($show_email == 0)
{
echo "Email:Hidden";
}
}
else
{
echo "Please login to view this page.";
}
?>

 

and a members page,

<?php

require_once 'db_connect.php';
require_once 'nav_bar.php';
require_once 'logged_in.php';

if ($_SESSION['is_valid'] == true){
if (isset($_GET['id'])) {
if ((int) $_GET['id'] > 0) {
$user_id = $_GET['id'];
$sql = "SELECT * FROM $user WHERE `user_id`='{$user_id}' LIMIT 0,1;";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$userprofname = $row['user_name'];
$profemail = $row['user_email'];
echo "$userprofname<br>";
$show_email = $row['show_email'];
if ($show_email == 1)
{ 
echo "Email:<a href='mailto:$profemail'>$email</a>";

}
elseif ($show_email == 0)
{
echo "Email:Hidden";
}
exit();
}
else {
echo "Invalid user ID passed to page! <br />";
echo "<a href=\"members.php\">Return to user list</a>";
exit();
}
}
//No ID passed to page, display user list:
$query = "SELECT user_id, user_name FROM $user";
$result = mysql_query($query) or die("Error:" . mysql_error());
if (mysql_num_rows($result) > 0) {
echo "User List:<br />";
while ($row = mysql_fetch_assoc($result)) {
  echo '<a href="?id=' . $row['user_id'] . '">' . $row['user_name'] . '</a><br />';
}
}
}
else
{
echo "Please login to view this page.";
}
?>

 

this is my user profile link

	<br><a href='user_profile.php?id=<?php echo $_SESSION['user_id']; ?>'>User Profile</a><br>

Link to comment
Share on other sites

well, with this you will get dynamic links that show

user_profile?id=1

etc.

this is my login code, showing how to add the sessions, and yes this will get you what you want

<?php
require_once 'includes/db_connect.php';

if ($_SESSION['is_valid'] == false){
if (isset($_POST['login'])){

$user_name = mysql_real_escape_string($_POST["user_name"]);		
$user_password =mysql_real_escape_string($_POST["user_password"]);	
$cookiename = forumcookie;   
$verify_username = strlen($user_name);
$verify_pass = strlen($user_password);
if ($verify_pass > 0 && $verify_username > 0)
{
$userPswd = md5($user_password);
$userpwsd = sha1($userPswd);
$sql = "SELECT * FROM $user WHERE user_name='$user_name' AND user_password='$userpwsd' LIMIT 1;";
$result = mysql_query($sql) or die(mysql_error(). " in $sql");
if (mysql_num_rows($result) == 1){
	$row = mysql_fetch_assoc($result);
	$user_level = $row['userlevel'];
	if ($user_level == 1) {
		$login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = 	'$_GET[p]'"));

		$userright = array($login_check['user_name'], $login_check['userlevel']);
		$s_userpass = serialize($userpass);
	$_SESSION['username'] = $row['user_name'];
	$_SESSION['user_password'] = $row['user_password'];
	$_SESSION['user_level'] = $row['userlevel'];
	$_SESSION['user_id'] = $row['user_id'];
	header("Location:http://".$_SERVER[HTTP_HOST]);
	$_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files
	 if(isset($_POST['remember'])){
    	  setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
    	  setcookie("cookpass", $_SESSION['user_password'], time()+60*60*24*100, "/");
   		}
	} 
		elseif ($user_level == 2){    
			$login_check = @mysql_fetch_array(mysql_query("SELECT * from `$user` WHERE user_name = '$_GET[u]' AND user_password = '$_GET[p]'"));

			$userright = array($login_check['user_name'], $login_check['userlevel']);
		$s_userpass = serialize($userpass);
	$_SESSION['username'] = $row['user_name'];
	$_SESSION['user_password'] = $row['user_password'];
	$_SESSION['user_level'] = $row['userlevel'];
	$_SESSION['user_id'] = $row['user_id'];
	header("Location:http://".$_SERVER[HTTP_HOST]);
	$_SESSION['is_valid'] = true; //change the session variable name to what you want, just remember it for all files
	 if(isset($_POST['remember'])){
    	  setcookie("cookname", $_SESSION['username'], time()+60*60*24*100, "/");
    	  setcookie("cookpass", $_SESSION['user_password'], time()+60*60*24*100, "/");
   		}
	}
}
else{
	echo "Login failed. Username and Password did not match database entries.";    
}
}

else
{
echo "Form was not completed. Please go back and make sure that the form was fully completed.";    
}
}
?> 

<html>
<table bgcolor='#999999' align='right'><form action="<?php $_SERVER['PHP_SELF']; ?>" method='POST'>
<tr><td>Username: </td><td><input type='text' name='user_name' /><br /></td></tr>
<tr><td>Password:</td><td> <input type='password' name='user_password' /><br /></td></tr>
<tr><td><input type="hidden" name="login" value="true"><input type="submit" value="Submit"></td></tr>
<tr><td><input type="checkbox" value="1" name="remember"> Remember Me </td></tr><tr><td><a href="register.php">[Register]</a></td></tr><tr><td><a href="forgot_password.php">[Forgot Password?]</a></td></tr></table>
</form>
</html>

<?php 
mysql_close();
}
else
{
header("Location:http://".$_SERVER[HTTP_HOST]);
}
?>

Link to comment
Share on other sites

The all idea of this is having a table with every user detail as column. So consider:

 

table 'users'

-----------

id | username | password | name | surname | email | gender | birthday | registered

 

When the user registeres he completes all the required info and if everything validates, is added to the db as a new row. Using sessions or cookies will keep track of his logged in state in all the site. For a profile page, profiles.php?user=seadog will do it, and the script gets the "user" url variable and queries the database for the related info. To make it profiles.php/seadog or even seadog.mysite.com u need to use htaccess to rewrite urls. There are a lot of tutorials about that, but this one ive found particulary interesting. Dont know what other questions u have, so feel free to ask them.

 

EDIT: Was writing this before Blade's code, but maybe u can still find it interesting.

Link to comment
Share on other sites

PHP hasnt such privileges actually. As far as i know, it can be done with htaccess, but as ive never experimented with such a technique, im not going to give my personal thoughts. Instead im giving two links where probably u'll find some good information: link1 and link2.

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.