Hi,
I am quite new to PHP and recently I have been attempting to create a login script, just one problem, the setcookie function isn't working.
I have tried a basic php file with nothing other than a function to set and retrieve the cookie but still nothing.
Why is this happening?
9 26258
hey ..
you make sure doing the right code for the sercookie -
examples:
-
-
-
setcookie("mycookie", $test, time() + 3600);
-
setcookie("mycookie","",time() - 3600);
-
-
setcookie('my_id', $_REQUEST['my_id'], time() + 60*60*24*365);
-
-
-
-
if still problem there plz mention..
Atli 5,058
Recognized Expert Expert
Hi.
There are a lot of reasons why thins could be happening, and simply stating that the function isn't working doesn't really narrow the list down.
We need a lot more details to be able to help with this.
Code examples and error messages are always helpful to.
I have tried all examples mentioned by "prabirchoudhur y" and none seem to work, everytime I try and retrieve the cookie, the page either comes up blank or displays Array ( ), depending upon the method I use to view them.
Unfortunately there are no error messages to provide an insight as to why this is happening.
P.S. I am using my own server which again I have only just begun to do, is there anything that I need to set in the PHP config file in order to get cookies to work?
Markus 6,050
Recognized Expert Expert
Are you sure that you have cookies enabled in your browser?
Atli 5,058
Recognized Expert Expert
Try this: - <?php
-
error_reporting(E_ALL);
-
ini_set("display_errors", true);
-
header("Content-Type: text/plain");
-
-
if(isset($_COOKIE['cookietest'])) {
-
var_dump($_COOKIE);
-
}
-
else {
-
$time = time();
-
setcookie("cookietest", $time, $time + 3600);
-
echo "Cookie set at: " . $time;
-
}
-
?>
When you first run it, it sets the cookie. Every request after that should show that cookie.
You shouldn't need to configure PHP in any way for this to work.
(I'm not even sure you can configure it to not support cookies :P)
If this doesn't work (if it keeps setting the cookie, rather than displaying it), then I'm guessing your browser doesn't support cookies.
Try using Firefox with the Web Developer plugin. It can show you all the cookies that are set on your browser, and if they are incorrectly set in some way.
I used the code you gave me and it seems to work, so I am now wondering, is the setcookie function the problem.
Here is my login script that I have tried to make, all of it works except for setting the cookie in any browser. -
<?php
-
$result = mysql_connect("*********", "******", "*********");
-
$result = mysql_select_db("test");
-
$user = $_POST["usr"];
-
$pass = $_POST["pss"];
-
$usr_stat = 0;
-
$pss_stat = 0;
-
$sql = "SELECT password FROM ".tbl_login." WHERE login = '$user';";
-
$result = mysql_query($sql);
-
$row_array = mysql_fetch_array($result);
-
$expire = time()+60*60*24*30;
-
-
if(!$result || (mysql_numrows($result) < 1)){
-
$usr_stat = 0;
-
}
-
else{
-
$usr_stat = 1;
-
}
-
if($pass == $row_array['password']){
-
$pss_stat = 1;
-
}
-
else{
-
$pss_stat = 0;
-
}
-
if($usr_stat == 1 && $pss_stat == 1){
-
setcookie ('curruser', $_POST['usr'], $expire);
-
header('Location: cktest2.php');
-
}
-
?>
-
<html>
-
<head>
-
<title>Studying PHP</title>
-
<link rel="stylesheet" href="css/main.css">
-
</head>
-
<body>
-
-
<h1>Login test</h1>
-
-
<form name="login" action="login.php" method="POST">
-
Username: <input type="text" value="" name="usr">
-
Password: <input type="password" value="" name="pss">
-
<input type="submit" value="Submit">
-
</form>
-
-
<?php
-
if(!$result || (mysql_numrows($result) < 1)){
-
echo "<p>Username invalid</p>";
-
}
-
else{
-
echo "<p>Username valid</p>";
-
}
-
if($pass == $row_array['password']){
-
echo "<p>Password valid</p>";
-
}
-
else{
-
echo "<p>Password invalid</p>";
-
}
-
if($usr_stat == 1 && $pss_stat == 1){
-
echo "<p>Login details valid</p>";
-
}
-
else{
-
echo "<p>Login details invalid</p>";
-
}
-
if(isset($_COOKIE['curruser'])){
-
echo "<p>Welcome ".$_COOKIE['curruser'].", you are now logged in using cookies.</p>";
-
}
-
else{
-
echo "<p>You are not logged in.</p>";
-
}
-
print_r($_COOKIE);
-
?>
-
-
</body>
-
</html>
-
Atli 5,058
Recognized Expert Expert
In the SQL on line #8 you use a constant 'tbl_login', but you don't set it anywhere.
Where does it come from?
Is this script being included into another script, or anything like that?
Keep in mind that the setcookie function doesn't work if you send anything to the output buffer before it is used.
Even a white-space before the <?php tag will cause the function to fail.
I tried your script with minor modifications to switch your DB validation to a static validation, and it worked fine.
This is the modified code I used: - <?php
-
if(isset($_POST["usr"])) {
-
//$result = mysql_connect("*********", "******", "*********");
-
//$result = mysql_select_db("test");
-
$user = $_POST["usr"];
-
$pass = $_POST["pss"];
-
$usr_stat = 0;
-
$pss_stat = 0;
-
//$sql = "SELECT password FROM ".tbl_login." WHERE login = '$user';";
-
//$result = mysql_query($sql);
-
//$row_array = mysql_fetch_array($result);
-
$expire = time()+60*60*24*30;
-
-
if(false/*!$result || (mysql_numrows($result) < 1)*/){
-
$usr_stat = 0;
-
}
-
else{
-
$usr_stat = 1;
-
}
-
if(true/*$pass == $row_array['password']*/){
-
$pss_stat = 1;
-
}
-
else{
-
$pss_stat = 0;
-
}
-
if($usr_stat == 1 && $pss_stat == 1){
-
setcookie ('curruser', $_POST['usr'], $expire);
-
header('Location: test.php');
-
}
-
}
-
?>
-
<html>
-
<head>
-
<title>Studying PHP</title>
-
<link rel="stylesheet" href="css/main.css">
-
</head>
-
<body>
-
-
<h1>Login test</h1>
-
-
<form name="login" action="test.php" method="POST">
-
Username: <input type="text" value="" name="usr">
-
Password: <input type="password" value="" name="pss">
-
<input type="submit" value="Submit">
-
</form>
-
<pre>
-
<?php
-
if(isset($_POST["usr"])) {
-
if($usr_stat == 0){
-
echo "<p>Username invalid</p>";
-
}
-
else{
-
echo "<p>Username valid</p>";
-
}
-
if($pss_stat == 0){
-
echo "<p>Password valid</p>";
-
}
-
else{
-
echo "<p>Password invalid</p>";
-
}
-
if($usr_stat == 1 && $pss_stat == 1){
-
echo "<p>Login details valid</p>";
-
}
-
else{
-
echo "<p>Login details invalid</p>";
-
}
-
}
-
if(isset($_COOKIE['curruser'])){
-
echo "<p>Welcome ".$_COOKIE['curruser'].", you are now logged in using cookies.</p>";
-
}
-
else{
-
echo "<p>You are not logged in.</p>";
-
}
-
-
print_r($_COOKIE);
-
?>
-
</pre>
-
</body>
-
</html>
Ahh, it's now working :)
I now know what the problem was thanks to Atli, I was using the constant where I shouldn't have, such simple things.
Sorry about that, i'll try and remember not to do that again, silly mistake.
Thanks for all your help.
Atli 5,058
Recognized Expert Expert
Glad you got it working.
Those little things, like spelling errors and such, always seem to cause the most trouble, don't they :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Craig Matthews |
last post by:
hello, any idea why the following code does not work?!!
<?
$cookieExpire = 864000000;
setcookie("a", "love", $cookieExpire);
header("Set-Cookie: lc=50; expires=$cookieExpire");
|
by: Phil Powell |
last post by:
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE and there is no value there! Guys, what
on earth do I do about this????
Here is the code that sets the cookie:
if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration', $row,
0, '/');
@mysql_free_result($query);
|
by: TG |
last post by:
What is wrong with the following code? I have set allow all cookies on for
my browser. Why does this always redirect to NoCookies.htm?
Please see code below (both of the logic constructs below always redirect to
NoCookies.htm)
===============================================================
setcookie ("thisisthename","Cookie_test",time()+86400,"/");
|
by: Ben |
last post by:
Hi all,
In my .php file, I'm using both session_start() and setcookie() before
<html> tag. It gives me following warning message:
Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\YC\songs.php:4) in
D:\Apache Group\Apache2\htdocs\YC\ycphpfunc.php on line 148
My .php file looks like this:
|
by: Katherine Hall |
last post by:
I am trying to set a cookie and use it later, but as soon as put a redirect
or anything, I loose it.
So when I first print it out, it exists. When I go to my redirect page it's
gone.
first page:
print_r($_COOKIE);
$url = "../somepage.php";
$target = "parent";
| |
by: Rewire |
last post by:
This script seems to work fine with firefox but not with IE. It just sets style="display:none" or style="display:" to show / hide submenu items on my menu. I've just tried making it save to a cookie which submenu is currently displayed and reopen that menu on a new page load.
checkCookie() is called onload and switchMenu(menuidhere) when the main menu item is clicked to make the submenu appear. Firefox doesn't display any js errors and...
|
by: LayneMitch via WebmasterKB.com |
last post by:
Hello.
Got another one for you folks. I'm working on this problem that wants me to
1. Prompt for name
2. Use pop-up box with name
3. Display current date on page in format "October 30, 2000."
4. Display last modified date of doc.
Here is my attempt. What a headache :-(
|
by: SupraFast |
last post by:
I have two hosting accounts. On one, my setcookie script works fine; cookies are created. On the other, the same script doesn't work. The function returns TRUE, but no cookies is created.
I checked to make sure that the variables have values and that the proper expire time is set and etc.
Any ideas?
<?php
foreach($_POST as $name => $value){
if($name == 'hItem'){
$item = $value;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |