473,382 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Why is cookie never set after login validation?

Ok, so far I have always used JSP and Servlets for my web programming, but now I needed to make use of PHP... and I'm stuck at something really basic: setcookie.

The form works fine, and it checks with the database for the username/pass ok (because when I supply good credentials I return back to the homepage, and with bad credentials it appends the error parameter with the URL).

Anyway, the cookie is never set. The browser never receives a cookie. This is my login.php.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host="*********"; // Host name
  3. $username="****"; // Mysql username
  4. $password="***"; // Mysql password
  5. $db_name="************"; // Database name
  6. $tbl_name="users"; // Table name
  7.  
  8. // Connect to server and select databse.
  9. mysql_connect("$host", "$username", "$password")or die("ERROR: Cannot connect to MySQL Server on 'localhost'.<br/><br/><i>Remember this is only a prototype demo!<br/>Franco</i>");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. // username and password sent from form
  13. $myusername=$_POST['uname'];
  14. $mypassword=$_POST['pass'];
  15.  
  16. // To protect MySQL injection (more detail about MySQL injection)
  17. $myusername = stripslashes($myusername);
  18. $mypassword = stripslashes($mypassword);
  19. $myusername = mysql_real_escape_string($myusername);
  20. $mypassword = mysql_real_escape_string($mypassword);
  21.  
  22. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  23. $result=mysql_query($sql);
  24.  
  25. // Mysql_num_row is counting table row
  26. $count=mysql_num_rows($result);
  27.  
  28. // If result matched $myusername and $mypassword, table row must be 1 row
  29. if($count==1){
  30. //Create cookie
  31.     $hour = time()+3600;
  32.     setcookie('ID_group_planner', $_POST['myusername'], $hour);
  33.     setcookie('Key_group_planner', $_POST['mypassword'], $hour);
  34.     header("location:index.php");
  35. }
  36. else {
  37. header("location:index.php?login_error=1");
  38. }
  39. ?>
Thanks a lot! Really appreciate your help at this.
Jan 26 '11 #1

✓ answered by Markus

See this note.

9 2157
are you sure there are no spaces at the beginning of your script, or anything that would be sent before the header(location...) is called?

cookies are sent along with headers, so make sure they are set before headers are sent. And make sure headers are not sent before you think they are... :)

let me know
Jan 26 '11 #2
Markus
6,050 Expert 4TB
See this note.
Jan 26 '11 #3
Well that is the whole script. There is absolutely nothing else in the login.php file. Unless I understood you incorrectly that is.
Jan 26 '11 #4
@Markus: Thanks! Let me try :)
Jan 26 '11 #5
No. Still doesn't create it...

Modified code is like this:

Expand|Select|Wrap|Line Numbers
  1. if($count==1){
  2. //Create cookie
  3.     $hour = time()+3600;
  4.     header("location:index.php");
  5.     setcookie('ID_group_planner', $_POST['myusername'], $hour);
  6.     setcookie('Key_group_planner', $_POST['mypassword'], $hour);
  7. }
  8. else {
  9. header("location:index.php?login_error=1");
  10. }
  11.  
Jan 26 '11 #6
Okay solved it. Problem lied in

Expand|Select|Wrap|Line Numbers
  1. $_POST['myusername']
  2.  
myusername isn't the name of a POST parameter but of fixed version of the username.

Thanks for the help though :)
Jan 26 '11 #7
I'm not sure, but i think you should better use your sanitized $myusername variable rather than the $_POST['anything'];
I don't know how that could be exploited but, i mean, if you already sanitized your variable, better use it...
Jan 26 '11 #8
Yep. In fact I fixed it to use $myusername instead of the original variable.

Thanks again guys! :)
Jan 26 '11 #9
Markus
6,050 Expert 4TB
You wouldn't have needed to post this question had you turned on error reporting ;)
Jan 27 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sarah Michael | last post by:
Hi, I am running the following code to set a cookie but it is not working, please help me sorting out the problem. The code is from an online article http://www.devarticles.com/art/1/639 I...
2
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
1
by: Glenn Venzke | last post by:
I have a pdf form that is submitting to an asp 3.0 page. I am setting a cookie value at login time that I need to access in this processing page. For some reason, the cookie is not being detected...
4
by: Tamer Higazi | last post by:
Hi! I wrote a small script setting a cookie.... but nothing is being set. What could be the problem?! Did I make something wrong?! One script is used to ask for the cookie and the other one shows...
0
by: Martin Bischoff | last post by:
Hi, I have a problem setting the style of a calendar control using style sheets. Example: I have set the calendar control's property TodayDayStyle-CssClass to "today". In my .css I have: ...
2
by: Greg | last post by:
Please note: I have cross posted this from Newsgroup: microsoft.public.dotnet.framework.aspnet.webservices with a few minor changes... I am having a simple problem setting up the security on my...
3
by: Paul | last post by:
I'm having a problem with the session being reset after setting a cookie. This is not supposed to happen. If I comment out the Add line below the site works normally, the session persists. Help. ...
3
by: MangroveRoot | last post by:
Hi all ... I've been learning and using PHP for a couple months now, working on a couple of somewhat different sites. On one site, I have a page with a form, and that form includes a couple of...
2
by: sophie_newbie | last post by:
Hi, I'm using the python to set a cookie when a user logs in. Thing is it doesn't seem to be setting properly in Internet Explorer. It works grand in Firefox. Its basically: c =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.