Support Forums

Full Version: I need help here
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello SF
I was creating my first website using php and i was doing great gathering the codes from here SF and from different tut around the internet.
I have created the database and the tables, the register page and the login page and everything is fine for now.

But after that i can't find how to show someone my username after i have logged in.

for example i need to see my username somewhere in the index.php page and i really appreciate if anybody can help me with the code.

Here are the codes:
login.php
PHP Code:
<?php
session_start
();
?>
<html>
<head>
<title>empty</title>
<meta name="keywords" content="empty">
<meta name="description" content="empty">
<link rel="shortcut icon" href="empty/favicon.ico">
<link href="empty/css_style.css" rel="stylesheet" type="text/css">
</head>

<body style="background:url('empty/img1.gif');" leftmargin="10" topmargin="10">

<table border="1" cellspacing="0" cellpadding="0" height="150" width="100%" bordercolor="#aabbbb">
<tr>
<td>
<?php include("header1.php"); ?>
</td>
</tr>
<tr>
<td>
<?php include("header2.php"); ?>
</td>
</tr>
<tr>
<td>
<?php include("header3.php"); ?>
</td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" height="100%" width="100%">
<tr>
<td style="background:#ffffff">
<?php include("mysql.php"); ?>
<form id="register" action="login.details.php" method="POST">
<table border="0" cellspacing="0" cellpadding="0" height="100%" width="30%" align="center">
<tr><td><font color="#000000">Username:</font></td><td><input type="text" name="loginusername" id="loginusername"/></td></tr>
<tr><td><font color="#000000">Password:</font></td><td><input type="password" name="loginpassword" id="loginpassword"/></td></tr>
<tr><td></td><td><input type="submit" value="Login" name="loginsubmit" id="loginsubmit" /></td></tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<?php include("buttom1.php"); ?>
</td>
</tr>
</table>
</body>
</html> 

login.details.php
PHP Code:
<?php
session_start
();
include(
"mysql.php");

$loginusername=$_POST['loginusername'];
$loginpassword=$_POST['loginpassword'];

// to protect from sql injection we use these
$loginusername stripslashes($loginusername);
$loginpassword stripslashes($loginpassword);
$loginusername mysql_real_escape_string($loginusername);
$loginpassword mysql_real_escape_string($loginpassword);

$checkuserdetailsexistance mysql_num_rows(mysql_query("select * from Users where Username='$loginusername' and Password='$loginpassword'"));
if (
$checkuserdetailsexistance == 1)
{
session_register("loginusername");
session_register("loginpassword");
header("location: index.php");//this is the page you want your login to goto so change the index.php part
}
else {
echo 
"Login failed. If you are not a member yet, please signup. Otherwise, check your spelling and login again.";
}
?>

Anybody can help me with the code please
thanks
Have you tried echo or print?
Surely you'd want to add slashes not strip them out...?? And what if someone puts a \ in their password?? You'll strip it out and break it.
(12-31-2009, 09:55 AM)Grizzly Wrote: [ -> ]Have you tried echo or print?

yes i tried the echo but it is not working. Nothing is shown on the index.php

I think that i should use some syntaxes related to session , i saw that in few examples on the internet but i don't know how to implement them.

But if there is another way, it would be better
(12-31-2009, 11:15 AM)MattR Wrote: [ -> ]Surely you'd want to add slashes not strip them out...?? And what if someone puts a \ in their password?? You'll strip it out and break it.

hmm i didn't test it with slaches. I will do some tests with other caracters to see how i can improve it.
But so far no one give me the solution for the 1st problem
No one can help me here? Sad
I have wrote this code in the index.php

PHP Code:
<?php
$username
=session_register("loginusername");
echo 
$username;
?>

and i can see "1" as a result on the index.php.
This mean that i am logged in the website but i can't figure out how to bring the informations such as the username to index.php page.

Anybody can help me with this?
Want a Cookie?

First you should have 2 variables for, mysql_query and mysql_num_rows.

PHP Code:
$query mysql_query("select * from Users where Username='$loginusername' and Password='$loginpassword'");
$checkuserdetailsexistance mysql_num_rows($query);
if (
$checkuserdetailsexistance == 1


Now when you execute the IF block, on the log-in success you should create a cookie along with the sessions.

PHP Code:
session_register("loginusername");
session_register("loginpassword");
// set the cookie with plain text username
setcookie("UserName"$loginusernametime() * 3600);

header("location: index.php");//this is the page you want your login to goto so change the index.php part
}
else {
echo 
"Login failed. If you are not a member yet, please signup. Otherwise, check your spelling and login again.";


All you need to do next, is to read the cookie if it exists.

PHP Code:
if(isset($_COOKIE['UserName'])) {
    
$username $_COOKIE['UserName'];


Your login.details.php would look like this now.

PHP Code:
<?php
session_start
();
include(
"mysql.php");

$loginusername=$_POST['loginusername'];
$loginpassword=$_POST['loginpassword'];

// to protect from sql injection we use these
$loginusername stripslashes($loginusername);
$loginpassword stripslashes($loginpassword);
$loginusername mysql_real_escape_string($loginusername);
$loginpassword mysql_real_escape_string($loginpassword);

$query mysql_query("select * from Users where Username='$loginusername' and Password='$loginpassword'");
$checkuserdetailsexistance mysql_num_rows($query);
if (
$checkuserdetailsexistance == 1)
{
session_register("loginusername");
session_register("loginpassword");

setcookie("UserName"$loginusernametime() * 3600);

header("location: index.php");//this is the page you want your login to goto so change the index.php part
}
else {
echo 
"Login failed. If you are not a member yet, please signup. Otherwise, check your spelling and login again.";
}
?>

And anywhere in the index.php you place this.

PHP Code:
if(isset($_COOKIE['UserName'])) {
    
$username $_COOKIE['UserName'];


EDIT:
Also session_register is deprecated function since 5.3.0 and removed in PHP 6
Take a look here for more information!
Smile i have figured that by myself like an hour ago but i am facing troubles when removing the cookies.
The fonction isn't working.

PHP Code:
$expirecookie=time()-3600;
setcookie('loginusername'""$expirecookie); 

what i am doing wrong?

And the server i am hosting my website still works with php v5

EDIT: Don't worry i don't have to remove the cookies

thank for your help. At least you are the only one that gave the right solution