So far, I have managed to create a register, login and welcome page with PHP. I'm now focusing on a creating a profile page. I'm wondering, first of all, how I can have every page on my site display "Welcome, " .$username. Do I have to insert a cookie write session_start(); on top of each page to do that, or is there an easier way?

And for the actual profile page, how can I make it display data from the database? I know it's fairly simple, but it doesn't work so far. I've tried something really simple, but lines 8, 10 and 14 are off. Thanks a lot for any help

BASICALLY, I'm asking how I can fetch data from my table just for the user that's logged in and display it.

<?php 
session_start();

$link = mysql_connect('localhost','root','1234') or die('Cant connect to database');
mysql_select_db('youtube');
	
$username=$_COOKIE['username'];
$username=$_COOKIE['password'];

$query = mysql_query("SELECT username FROM user WHERE username='$username' 
AND password='$password'")
or die(mysql_error());

echo "Welcome, " . $username . ".";
echo $email;
//echo a bunch of other stuff, such as the user's age, real name and so forth

if (!isset($_SESSION['username']))
{
	header('location: login.php');
}
?>

Recommended Answers

All 3 Replies

Please forget the previous code. I've changed it to this and have NO idea why it's not working (it's not posting the full name):

<?php 
session_start();
include "config.php";
	
echo "Welcome, " . $_SESSION['username'];
$result = mysql_query("SELECT username FROM user WHERE username='$username'")
or die(mysql_error());
$row=mysql_fetch_array($result);

echo "Hi, " . $row['full_name'];

if (!isset($_SESSION['username']))
{
	header('location: login.php');
}
?>
<p>Would you like to edit your <a href="editprofile.php">profile?</a><p>

Thanks again!

Noticed a few things.

<?php 
session_start();
include "config.php";

if (!isset($_SESSION['username']))
{
	header('location: login.php');
}

echo "Welcome, " . $_SESSION['username'];
$result = mysql_query("SELECT username, full_name FROM user WHERE username='$username'")
or die(mysql_error());
$row=mysql_fetch_array($result);

echo "Hi, " . $row['full_name']; // You haven't selected "full_name"

?>
<p>Would you like to edit your <a href="editprofile.php">profile?</a><p>

What error(s) do you have? I'm guessing that the row "full_name" is storing two attributes inside i.e. "Joe Bloggs" this is bad database design!

Also if you're doing profiles (I'm guessing you want other people to view these profiles?)

You'll need to write another script that handles it, there are many ways but:

www.yoursite.com/user/view_profile.php?id={user_id}

<?php

     $id = $_GET['id'];

     echo "Viewing profile for, $id";
?>

Then you can write sql select statements that selects all the information depending on the user id.. Look at like SQL injections so it's a lot more secure!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.