Jump to content

Help please!!


ckerr27

Recommended Posts

Hey, i have created a website an have a login feature working with username and password from a mysql database. I have been trying to get the username to appear on the home page when the user logs in

 

e.g. Welcome ckerr27

 

Any help would be much appreciated!

 

Thanks

 

Connor

Link to comment
Share on other sites

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password=""; // Mysql password

$db_name="test"; // 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 $username and $password, table row must be 1 row

 

if($count==1){

// Register $username, $password and redirect to file "home.html"

session_register("myusername");

session_register("mypassword");

header("location:home.html");

}

else {

echo "Wrong Username or Password";

}

?>

 

 

This is the checklogin.php file. when the login has been aproved the user is redirected to the home.php page

Link to comment
Share on other sites

So you will want to ad the username onto the querystring if the page to be redirected to:

 

header("location:home.html?username=" . $myusername);

 

then in home.php you will grab the value using $_GET['username'];

Link to comment
Share on other sites

Thanks,

 

I put this code into checklogin.php

 

if($count==1){

// Register $username, $password and redirect to file "home.html"

session_register("myusername");

session_register("mypassword");

header("location:home.php username=" . $myusername);

}

 

Getting this error message

 

The requested URL /website/home.php username=admin was not found on this server.

 

Sorry its probably easy fixed

 

Thanks again

Link to comment
Share on other sites

You don't want to be stripping slashes from a password as it could contain a slash. Also, if you store your passwords as a hash you don't have to worry about SQL Injection from the password POV as it'll get hashed when you check it.

Link to comment
Share on other sites

you have a space in the URL instead of a question mark (?).

The question mark starts the query string of the URL.

 

Use:

 

header("location:home.php?username=" . $myusername);

 

NOT

 

header("location:home.php username=" . $myusername);

Link to comment
Share on other sites

<html>

<head>

<link rel="stylesheet" type ="text/css" href="anything2.css"

</head>

 

<body>

 

 

<div id="container">

 

<div id="header"><img src="imagesb.jpg" alt="Cool Image" align="left">

<img src="images.jpg" alt="Cool Image" align="right"><center><b><font size="6.5"><br><br>G.O.A.C.A Home Page</b></center></font>

 

</div>

 

 

<div id="leftnav"><center>

$_GET['username'];

<br><br>

<input type= "button" style="width:120px;" value="Personal Details" onClick="window.location= 'personaldetails.php' ">

<br><br>

<input type= "button" style="width:120px;" value="Club Details" onClick="window.location= 'clubdetails.php' ">

<br><br>

<input type= "button" style="width:120px;" value="Future Events" onClick="window.location= 'futureevents.php' ">

<br><br>

<input type= "button" style="width:120px;" value="News" onClick="window.location= 'news.php' ">

<br><br>

<input type= "button" style="width:120px;" value="FAQ" onClick="window.location= 'faq.php' ">

<br><br>

<input type= "button" style="width:120px;" value="Wall" onClick="window.location= 'wall.php' ">

<br><br>

<input type= "button" style="width:120px;" value="About Us" onClick="window.location= 'about.php' ">

</div>

 

<div id="body">

<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>

<A HREF = index.php>Log out</A>

 

 

 

 

<div id="footer">Welcome to the Glens of Antrim Chess Association Homepage!!!!</div>

 

</body>

</html>

 

Link to comment
Share on other sites

well you are sending $myusername to home.php, which comes from this line in the checklogin page: $myusername=$_POST['myusername'];

So it should contain the username that the user enters.

Check to see if register_globals is turned on, place this code at the top of the page.

<?php echo "Register Globals: " . ini_get('register_globals'); ?>

Link to comment
Share on other sites

I might be totally wrong here, but I don't see a point of passing the username through a get request if you are already storing it in a session variable!

 

Instead of session_register, do a $_SESSION['myusername'] = $myusername;

 

and on the second page, just do

 

<?php
echo $_SESSION['myusername'];
?>

 

No need to play with GET requests!

Link to comment
Share on other sites

That worked perfectly thanks. I kept the session register but the echo worked perfectly. now when i try to log out and login with a different user the first username is still displayed.

 

I have a logout function...

 

<?PHP

session_destroy();

?>

 

Is this the correct code to use when the logout link is pressed?

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.