Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Username as Greeting (Welcome, [username]

Status
Not open for further replies.

Skippie1

Technical User
May 7, 2012
29
0
0
ZA
Hi,

I am terrible with PHP. I am using Dreamweaver. I have a login page where users can log in fine and then gets redirected to another page. I want to use the username of the member of my website just used to log in to create a Welcome greeting. So if the username = Stefan it must show Welcome, Stefan.

How can this be done. I am using a SQL database where all my members is in. If somebody can help me it would be great.
 
Are you using a session?

How are you stopping people from simply loading the page you are redirected to without going through the login page?

Do you keep something related to the user somewhere??



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi,

Here is the PHP script I am using at the login page, as far as I know I must use $_SESSION['MM_Username'] to create a welcome greeting but I added it but it just say Welcome, (and nothing further)

The other problem is that people can access the "protected page" by just typing oin the URL.

<?php require_once('Connections/Bloembuz.php'); ?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['user'])) {
$loginUsername=$_POST['user'];
$password=$_POST['password'];
$MM_fldUserAuthorization = "";
$MM_redirectLoginSuccess = "uploadftp.php";
$MM_redirectLoginFailed = "index.html";
$MM_redirecttoReferrer = false;
mysql_select_db($database_Bloembuz, $Bloembuz);

$LoginRS__query=sprintf("SELECT Username, Password FROM Users WHERE Username=%s AND Password=%s",
GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text"));

$LoginRS = mysql_query($LoginRS__query, $Bloembuz) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
if ($loginFoundUser) {
$loginStrGroup = "";

if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
//declare two session variables and assign them
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;

if (isset($_SESSION['PrevUrl']) && false) {
$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
else {
header("Location: ". $MM_redirectLoginFailed );
}
}
?>
 
In the page where you want to display the user name, you need to start the session to, so use session_start() there before attempting to use the $_SESSION variable.

Code:
session_start();
echo "Welcome " . $_SESSION['MM_Username'];

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi, this is working 100%. Now how do I cancel the session once the user close the page and how do I redirect people to the login page if they are trying to type in the URL.
 
Hi, this is working 100%. Now how do I cancel the session once the user close the page

If the user closes the browser, the session will be destroyed automatically.

how do I redirect people to the login page if they are trying to type in the URL.

Check for the existence of a session var like $_SESSION['isloggedin'] or something you set in your login page along with the Usenamer and UserGroup variables.
If it doesn't exist, then redirect back to login, via the header(location:) call;
Code:
...
$_SESSION['MM_Username'] = $loginUsername;
$_SESSION['MM_UserGroup'] = $loginStrGroup;	
[red]$_SESSION['isloggedin'] = true;[/red]

Code:
session_start();
if(!isset('isloggedin']))
{
header("Location:[b][COLOR=#0066aa]loginpage.php[/color][/b]");
}



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks for all the help so far.

I am using FTP to upload files from a webpage to my server. It is already working but I want the user that is loggin in to upload to his own directory and I want the image name changed from whatever he called it to for example "image 1".

Currently I have a "upload" directory on my server so if user "ABC" log in I want that images to be saved at "upload/ABC".

Can you help me with this one?
 
Take a read of the PHP online manual entry for Handling Uploaded files. There's a function called [link php.net/manual/en/function.move-uploaded-file.php]move_uploaded_file()[/url] that you can use to move them to the specified location.

[link php.net/manual/en/features.file-upload.post-method.php]Upload File Example[/url]

Simply use yuor Username session var in the path to get it to upload where you want it.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi,

I tried to add the path as follows but it gives me a syntax error:

$paths="httpdocs/upload/. $_SESSION['MM_Username']";
 
Try:

Code:
$paths="httpdocs/upload/[red]"[/red]. $_SESSION['MM_Username'];

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top