Jump to content

Recommended Posts

Hi guys

 

I am new to PHP and need som help. I have set up a site that allows a user to log in through a simple form where the data is then send to checklogin.php. Here the data is checked up against my sql database and if the login is correct the user is transfered to the "secret" members only site. All this works fine.

My question is then, how do I get the members site to greet the member with "Hello 'username'"; of course where the username changes depending on the login.

This is the part where the username and password is checked:

 

<?php

$host="mydbb10.surftown.dk"; // Host name

$username="****"; // Mysql username

$password="****"; // Mysql password

$db_name="****"; // Database name

$tbl_name="members"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// username and password sent from form

$myusername=$_POST['myusername'];

$mypassword=$_POST['mypassword'];

 

// To protect MySQL injection (more detail about MySQL injection)

$myusername = stripslashes($myusername);

$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);

$mypassword = mysql_real_escape_string($mypassword);

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "korrekt.php"

session_register("myusername");

session_register("mypassword");

echo "Tak fordi du loggede ind<br>Redirecter...";

header("location: ../forhandlerservice.php");

}

else {

echo "Forkert brugernavn eller password";

header("location: ../loginfejl.html");

}

?>

 

 

and this is the first line of code on the members site:

 

<?

session_start();

if(!session_is_registered(myusername)){

header("location:login.html");

}

?>

 

 

Sorry if I provided to much code, just want to make sure that I don't forget anything.

 

 

Any help is appreciated. Thank you

Link to comment
https://forums.phpfreaks.com/topic/251483-showing-the-users-name-after-login/
Share on other sites

Looks like your using sessions already, try this:

 

// On the page where you check for correct log in information
session_start();
$_SESSION['username'] = 'actualUsername';

 

// On the page where you want to use the username
session_start();
echo "Hello". $_SESSION['username'];

Thanks for the reply. I actually figured it out by adding this block of code:

<?php

echo "Hello " . $myusername;

?>

, so that was basically the same you suggested.

My next question is then, how do I register something in the session that is not contained in the form. Meaning that in my database I have username, password and real name. It would obviously look better if I could get the code to print out "Hello "Real name"".

I hope it makes sense, and again, thanks for your answer.

In my database I have username, password, email and real name.

I assume I have to register username and password in order to verify the user is logged in. Furthermore I would like to register real name so that I can print out: Hello "Real name".

The way to Register session

<?php
// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");

// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["zim"] = "An invader from another planet.";

// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
?>

 

Now use sql query like

$query = 'SELECT username,password,realname FROM yourtable where username ='.$_POST['username']; //if username is used for loggingin
$result = mysql_query($query , $db);
$row = mysql_fetch_assoc($result);
$us = $row['username'];
$pa = $row['password'];
$rn = $row['realname'];

 

Now use these variables to register as shown above.

Hope this would help if not i am here i will explain more

Thanks, but I'm pretty lost on the whole register thing :)

Given the code I provided how would I go about implementing it there.

The thing is this. I have one page with a form requiring username and password that when submitted posts the data to checklogin.php. The username and password is then checked against my database and if correct the user is redirected to the member site, where the first line of code is:

<?

session_start();

if(!session_is_registered(myusername)){

header("location:login.html");

}

?>

 

Following this php code is a whole bunch of html showing the actual member site. Inside this html I have then added:

<div id='splitter2'>

<?php

echo "Hello " . $myusername;

?>

</div>

, which at the current time prints out: Hello test123 (e.g.).

I would then like it to instead of printing out the username which is usually something corny I would like it to print out Hello John (e.g.), where John in the database is associated with the username test123.

to follow up using killervastu's code:

 

use this code on the page you plan on using the real name

$query = 'SELECT `realname` FROM yourtable where username ='.$_POST['username']; //if username is used for loggingin
$result = mysql_query($query , $db);
$row = mysql_fetch_assoc($result);
$realName = $row['realname'];

// sotre it in your session if you want to use it elsewhere...
session_register("realName"); 

// Further down somehwere on your page:...
echo "Hello ". $realName;

 

when the username and password is correct use this in login.php

if username and password match then

 $sql = mysql_query("SELECT * FROM yourtable WHERE uniqueid =".$username." AND password ='".$password."'" ,$db) or die(mysql_error());
				 $row = mysql_fetch_array($sql) or die(mysql_error());

				$usernamec = $row['uniqueid'];
                                        $name = $row['name'];
				$_SESSION['uniqueid'] = $usernamec;
				$_SESSION['logged'] = 1;
                                        $_SESSION['name'] = $name;

now in member .php

use this

<?php

session_start();

if(!isset($_SESSION['logged']) || $_SESSION['logged'] != 1)
	{
		header('Refresh: 5; URL=login.php?redirect='. $_SERVER['PHP_SELF']);
		echo '<p>You will be Redirected to login page within 5 sec.</p>';
		echo '<p> IF the browser Dose not redirect <a href="login.php?redirect='. $_SERVER['PHP_SELF'] . '">Click here</a></p>';
		die();
	}
?>

instead of this

<?
session_start();
if(!session_is_registered(myusername)){
header("location:login.html");
}
?>

 

now you can easily display the name

using

echo 'Welcome '.$_SESSION['name'];

 

to follow up using killervastu's code:

 

use this code on the page you plan on using the real name

$query = 'SELECT `realname` FROM yourtable where username ='.$_POST['username']; //if username is used for loggingin
$result = mysql_query($query , $db);
$row = mysql_fetch_assoc($result);
$realName = $row['realname'];

// sotre it in your session if you want to use it elsewhere...
session_register("realName"); 

// Further down somehwere on your page:...
echo "Hello ". $realName;

You can do it like this also if you wish

if you feel not to use session_register() use the above post's script

session_register was depreciated over 9 years ago (in favor of $_SESSION variables), finally throws a depreciated error messages in php5.3, and has been removed as of php 5.4. DON'T use session_register in your code and any code that current uses it must be updated to use $_SESSION variables.

Thanks for all the answers, but I must be stupid. I can't figure this out.

 

The three pages I have in this loop are: login.html (with the form), checklogin.php (where the data from the form is send to and checked against the database) and members.php (where the user is send to if login is correct - otherwise user is send back to login.html).

 

I tried adding the code that you suggested but quite honestly I didn't know what segments to take out and replace with your code.

I really appreciate your help guys but I really suck at php so please bare with me :)

 

If you guys wouldn't mind if would be easier for me to see my own code altered instead of correct code posted as I don't know where to put it in  :wtf:

 

 

Thanks again, and sorry for not understanding :)

The code you posted is obviously from phpeasystep.com, and it is obsolete and outdated. I would not using that site to attempt to learn. Their code is at least 7-8 years behind the curve, and will quite likely cease to function at all with future versions of PHP.

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.