This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-12 15:22:00

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

PHP Cookies not working

I have this code that logs the user in:

Code:

<?php 
session_start(); // Shows we are using sessions 

$dbHost = '*';
$dbUser = '*'; 
$dbPass = '*';
$dbname = '*'; // the database you put the table into.

$username = $_POST['username']; // Gets the inputted username from the form 
$password = $_POST['password']; // Gets the inputted password from the form 
$time = time(); // Gets the current server time 
$check = $_POST['setcookie']; // Checks if the remember me button was ticked 

$db = mysql_connect($dbHost,$dbUser,$dbPass); // Connection Code 
mysql_select_db($dbname,$db); // Connects to database 

$query = "SELECT user, pass FROM login WHERE user = '$username' AND pass = '$password'"; 
$result = mysql_query($query, $db); 
if(mysql_num_rows($result)) { // If the username and password are correct do the following; 
$_SESSION['loggedin'] = 1; // Sets the session 'loggedin' to 1 

if($check) { 
// Check to see if the 'setcookie' box was ticked to remember the user 
setcookie("username", $username, $time + 3600); // Sets the cookie username 
setcookie("password", $password, $time + 3600); // Sets the cookie password 
} 
$ip = $_SERVER['REMOTE_ADDR'];
$d = date('D');
$m = date('M');
$D = date('d');
$y = date('y');
$date = "$d,$D,$m,20$y ";
mysql_query("INSERT INTO successful_logins (ip, time, username, password)
VALUES ('$ip', '$date', '$username', '$password')");
header('Location: http://www.plaxon.comyr.com/admin.php'); // Forwards the user to this URL 
exit(); 
} 
else // If login is unsuccessful forwards the user back to the index page with an error 
{ 
$ip = $_SERVER['REMOTE_ADDR'];
$d = date('D');
$m = date('M');
$D = date('d');
$y = date('y');
$date = "$d,$D,$m,20$y ";
mysql_query("INSERT INTO failed_logins (ip, time, username, password)
VALUES ('$ip', '$date', '$username', '$password')");
header('Location: http://www.plaxon.comyr.com/login.php?error=true'); 
exit(); 
}
?>

And this project upload script:

Code:

<link rel="stylesheet" type="text/css" href="global.css" />
<?php
$dbHost = '*';
$dbUser = '*'; 
$dbPass = '*';
$dbDatabase = '*''; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database. Error: " . mysql_error());
?>
<?php 
include "demo.html";
?>
<?php
session_start(); 

if(!isset($_SESSION['loggedin'])) { 
// If the session 'loggedin' is NOT set forward the user back to the login form with the error set 
header('Location: http://www.plaxon.comyr.com/error2.php'); 
exit(); // Otherwise show the rest of the page (admin section) 
}
echo "<h1>Upload a Project: <a href=\"info.upload.php\">[+]</a></h1>";

$form = "<form action='upload.php' method='POST' enctype='multipart/form-data'>
<table>
<tr>
    <td><input type='text' name='planetname'></td>
</tr>
<tr>
    <td><input type='file' name='myfile'></td>
</tr>
<tr>
    <td><input type='submit' name='submitbutton' value='Submit'></td>
</tr>
</table>
</form>";

if ($_POST['submitbutton'])
{
    $ip = $_SERVER['REMOTE_ADDR'];
    $planetname = $_POST['planetname'];
    $name = $_FILES['myfile']['name'];
    $type = $_FILES['myfile']['type'];
    $size = $_FILES['myfile']['size'];
    $tmpname = $_FILES['myfile']['tmp_name'];
    $ext = substr($name, strrpos($name, '.'));
    $file = "$name";
    
    if ($ext == ".sb")
    {
    if ($_FILES["myfile"]["error"] > 0)
    {
    echo "<p>Return Code: " . $_FILES["myfile"]["error"] . "</p><br />";
    }
    else
    {
    if (file_exists("planets/" . $_FILES["myfile"]["name"]))
      {
      echo "<p>Your planet " . $_FILES["myfile"]["name"] . " has already been shared. Please <a href=\"upload.php\">retry</a>.</p>";
      }
    else
      {
        $username = $_COOKIE["username"];
        $uploads_dir = 'planets';
        move_uploaded_file($tmpname, "$uploads_dir/$name");
        mysql_query("INSERT INTO project_details (name, file, creator, creator_ip)
VALUES ('$planetname', '$file', '$username', '$ip')");
        $link = mysql_connect("[removed by moderator]", "[removed by moderator]", "[removed by moderator]");
mysql_select_db("a5292800_users", $link);

        $result = mysql_query("SELECT * FROM project_details", $link);
        $num_rows = mysql_num_rows($result);

        $planetid = mysql_query("SELECT LAST(projectid) FROM project_details");
      echo "<p>Your planet has been shared! You can find your planet <a href=\"planets.php\">here</a>!</p>";
      }
    }
  }
  else
  {
  echo "You can only share SB planet files. Please <a href=\"upload.php\">retry</a>.";
  }
}
else
{
echo $form;
}
?>

It adds the project details to database, but it doesn't read the $_COOKIE["username"] part, and it just leaves that field blank? I search for PHP cookies and the $_COOKIE["cookie"] was there, but it doesn't work  sad

Last edited by Paddle2See (2011-10-12 20:55:45)


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#2 2011-10-12 15:55:18

bbbeb
Scratcher
Registered: 2009-06-11
Posts: 1000+

Re: PHP Cookies not working

Code:

$dbPass = *';

1 error in #1

And which one is that code in again?


Back in my day.... there were no laws that censored the internet... now, there are.... nah.

Offline

 

#3 2011-10-12 15:59:02

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

bbbeb wrote:

Code:

$dbPass = *';

1 error in #1

And which one is that code in again?

That's not the error. I just hightlighted the ' when I was putting in the *. Anyway, the error is in the second code.


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#4 2011-10-13 22:15:41

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: PHP Cookies not working

Hmm, I don't see anything that's wrong. Try again and tell us the results?

Offline

 

#5 2011-10-14 02:56:14

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

I can't. There are no results. It just doesn't read the cookie, and adds a blank field to mysql databse.


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#6 2011-10-14 02:56:20

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: PHP Cookies not working

couldn't you use $_SESSION variables?
http://www.w3schools.com/php/php_sessions.asp


http://i.imgur.com/zeIZW.png

Offline

 

#7 2011-10-14 03:18:36

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

rookwood101 wrote:

couldn't you use $_SESSION variables?
http://www.w3schools.com/php/php_sessions.asp

not working either.


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#8 2011-10-14 10:26:55

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: PHP Cookies not working

have a look at this tutorial it's been very useful to me in the past, and I know it works, as I have used it.


http://i.imgur.com/zeIZW.png

Offline

 

#9 2011-10-14 10:34:04

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: PHP Cookies not working

WindowsExplorer wrote:

I can't. There are no results. It just doesn't read the cookie, and adds a blank field to mysql databse.

Well, I logged in and then looked at my cookies, and the cookie was there, then I did a test on my own website and it worked.

Also, I suggest saving files to just the /planets folder, you would need to make another folder for each user if you were to do that.

I suggest making a test file uploader, then add things until you find the broken piece of code.

Offline

 

#10 2011-10-14 17:53:34

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

rookwood101 wrote:

have a look at this tutorial it's been very useful to me in the past, and I know it works, as I have used it.

Great tutorial, but could you give me the link to the source file to that?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#11 2011-10-15 03:42:09

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

WindowsExplorer wrote:

rookwood101 wrote:

have a look at this tutorial it's been very useful to me in the past, and I know it works, as I have used it.

Great tutorial, but could you give me the link to the source file to that?

?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#12 2011-10-15 04:06:12

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: PHP Cookies not working

WindowsExplorer wrote:

WindowsExplorer wrote:

rookwood101 wrote:

have a look at this tutorial it's been very useful to me in the past, and I know it works, as I have used it.

Great tutorial, but could you give me the link to the source file to that?

?

I think reading the tutorial will help you better understand it. It does have the code somewhere in it, but the point is, if you read through it, you can digest the information better, and see what it's all for.


http://i.imgur.com/zeIZW.png

Offline

 

#13 2011-10-15 04:19:09

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

rookwood101 wrote:

WindowsExplorer wrote:

WindowsExplorer wrote:


Great tutorial, but could you give me the link to the source file to that?

?

I think reading the tutorial will help you better understand it. It does have the code somewhere in it, but the point is, if you read through it, you can digest the information better, and see what it's all for.

I know, but it says teh code will be at the end, and it isn't, so I was wondering if you knew.


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#14 2011-10-15 05:47:06

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: PHP Cookies not working

WindowsExplorer wrote:

rookwood101 wrote:

WindowsExplorer wrote:


?

I think reading the tutorial will help you better understand it. It does have the code somewhere in it, but the point is, if you read through it, you can digest the information better, and see what it's all for.

I know, but it says teh code will be at the end, and it isn't, so I was wondering if you knew.

If you read the whole thing, you won't need the code.

Anyhow, the code is spread throughout the tutorial, but a lot of it is on 'page 2' or page 3


http://i.imgur.com/zeIZW.png

Offline

 

#15 2011-10-15 07:39:16

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

rookwood101 wrote:

WindowsExplorer wrote:

rookwood101 wrote:


I think reading the tutorial will help you better understand it. It does have the code somewhere in it, but the point is, if you read through it, you can digest the information better, and see what it's all for.

I know, but it says teh code will be at the end, and it isn't, so I was wondering if you knew.

If you read the whole thing, you won't need the code.

Anyhow, the code is spread throughout the tutorial, but a lot of it is on 'page 2' or page 3

Yes, but where is the login and signup form code?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#16 2011-10-15 08:36:55

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Offline

 

#17 2011-10-15 08:43:43

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: PHP Cookies not working

there not there?


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

Board footer