Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there! I have a login page on my website. If the password and username is correct it creates a cookie :
PHP
//create cookie
$date_of_expiry = time() + 300 ;
setcookie( "userlogin", " ". $_POST['myusername'] ."", $date_of_expiry );


and after it creates a cookie it redirects the user: header('location:shopowner.php');

On shopowner.php the code is supposed to display MySQL data using the value of the cookie:
PHP
<?php

$connect = mysql_connect("localhost", "root") or die (mysql_error());

mysql_select_db("dresses", $connect);
echo "Welcome " . $_COOKIE['userlogin'] . "<br>";

$sql = "SELECT * FROM data WHERE Username = '" . $_COOKIE['userlogin']  . "'";
$result = mysql_query($sql, $connect);
echo 'MyData:<br>';
while ($row = mysql_fetch_array($result)) {
echo   "" . $row['Shop'] . "<br>" ;
}

?>

There are no error messages that come up. the cookies work because it echo's: Welcome Admin(the value of the cookie) but no MySQL data gets displayed. If anyone would be kind enough te help me figure out what the problem is.
Posted

1 solution

Seems like the value of the cookie is an empty space concatenated with the username:
PHP
" ". $_POST['myusername'] .""


Is your username in database 'admin' or ' admin'?

Change your code when setting the cookie to this simpler version:
PHP
//create cookie
$date_of_expiry = time() + 300 ;
setcookie( "userlogin", $_POST['myusername'], $date_of_expiry );
 
Share this answer
 
v2
Comments
UlrichvL 12-Oct-13 2:10am    
The username in database is admin.
Homero Rivera 12-Oct-13 2:12am    
because of how you are setting your cookie, you are telling the database to look for ' admin' instead of 'admin'. ' admin' does not exist, so your database returns no results.
UlrichvL 12-Oct-13 2:13am    
Wil go and check it out and wil return back to you with the result
UlrichvL 12-Oct-13 2:12am    
If the empty space is taken away an error message appears:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\Dropshop\Main home\signin.php on line 108
UlrichvL 12-Oct-13 2:17am    
Thank you very much for you're kind help! You were right! It works now that I took away the empty space, I wouldn't have seen that on my own. Many thanks.
Ulrichvl

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900