PHP session not working correctly

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

76 Replies - 11009 Views - Last Post: 01 April 2011 - 04:01 PM Rate Topic: -----

#1 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

PHP session not working correctly

Posted 23 March 2011 - 03:33 AM

I have made a login page for my website which now works (with the help of members of this forum) but I have noticed if I go directly to the page it will let me in regardless whether I'm logged in or not.

the script im using is:

<?php
$host="localhost"; // Host name
$username="blank"; // Mysql username
$password="blank"; // Mysql password
$db_name="blank"; // 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 $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
       else {
Print "<html>";
Print "<head>";
Print "<link href='styles.css' rel='stylesheet' type='text/css' />";
Print "</head>";
Print "<body>";
Print "<img src=images/banner2.png  alt='banner' />";
Print "<p>Wrong Username or Bad Password.</p>";
Print "<p>Or <a href='./index.html'>Click Here</a> to try again.</p>";
Print "</body>";
Print "</html>";
}
?>



and on each page I have:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>



now I've read that this method is now depriciated, but I don't know how to change it to the new method, so if I can get my method working it wouldn't bother me if its the old one, i'd just be very grateful as I have been working on this script for nearly two days and its starting to become a headache!

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: PHP session not working correctly

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: PHP session not working correctly

Posted 23 March 2011 - 03:39 AM

after starting a session, you handle all the (to be) saved variables via the $_SESSION superglobal.
session_start();
if (isset($_SESSION['some_key']))
{
	doSomethingWith($_SESSION['some_key']); 
}

Was This Post Helpful? 0
  • +
  • -

#3 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP session not working correctly

Posted 23 March 2011 - 04:08 AM

View PostDormilich, on 23 March 2011 - 03:39 AM, said:

after starting a session, you handle all the (to be) saved variables via the $_SESSION superglobal.
session_start();
if (isset($_SESSION['some_key']))
{
	doSomethingWith($_SESSION['some_key']); 
}


so 'some key' will be password? i'm sorry I'm not very good at PHP, this is just based off a script on the phpeasy website..
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: PHP session not working correctly

Posted 23 March 2011 - 04:50 AM

some_key is the "name" of your session variable (like myusername)
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: PHP session not working correctly

Posted 23 March 2011 - 04:55 AM

You might consider putting the effort in to learn how PHP works. Go read the manual. This here is the Session reference.
Was This Post Helpful? 0
  • +
  • -

#6 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP session not working correctly

Posted 23 March 2011 - 05:00 AM

View PostDormilich, on 23 March 2011 - 04:50 AM, said:

some_key is the "name" of your session variable (like myusername)


and whats the dosomethingwith? lol

View PostJackOfAllTrades, on 23 March 2011 - 04:55 AM, said:

You might consider putting the effort in to learn how PHP works. Go read the manual. This here is the Session reference.


i've read tutorials, and had a look at the manual already, the problem is if it doesn't sink in, it doesn't sink in. I have tried, look at my other posts, I've been trying to sort out this script for 2 days now :(
Was This Post Helpful? 0
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: PHP session not working correctly

Posted 23 March 2011 - 05:29 AM

EDIT: I see it now. thanks Dormilich. session_register is deprecated anyways

$_SESSION is just an array if it makes it any easier for you.

This post has been edited by codeprada: 23 March 2011 - 05:52 AM

Was This Post Helpful? -1
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: PHP session not working correctly

Posted 23 March 2011 - 05:31 AM

View Postcodeprada, on 23 March 2011 - 01:29 PM, said:

Change lines 31 & 32 to

session_register($myusername);
session_register($mypassword);

actually, the syntax is session_register("myusername"); and session_is_registered("myusername");

This post has been edited by Dormilich: 23 March 2011 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#9 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP session not working correctly

Posted 23 March 2011 - 06:06 AM

i've done what you've told me but I can still access the pages without logging in...
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: PHP session not working correctly

Posted 23 March 2011 - 06:10 AM

write var_dump($_SESSION); instead of the redirect (temporarily). it seems like you donít set those variables correctly.
Was This Post Helpful? 0
  • +
  • -

#11 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: PHP session not working correctly

Posted 23 March 2011 - 06:10 AM

View PostAddio569, on 23 March 2011 - 09:06 AM, said:

i've done what you've told me but I can still access the pages without logging in...


I'd honestly suggest you use $_SESSION variables and cookies instead of session_register.
Was This Post Helpful? 1
  • +
  • -

#12 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP session not working correctly

Posted 23 March 2011 - 06:14 AM

View PostDormilich, on 23 March 2011 - 06:10 AM, said:

write var_dump($_SESSION); instead of the redirect (temporarily). it seems like you donít set those variables correctly.


like this:

<?php
session_start()
if(!session_is_registered($_SESSION['username'])){

{

    var_dump($_SESSION);
    
}
?>


??

View Postcodeprada, on 23 March 2011 - 06:10 AM, said:

View PostAddio569, on 23 March 2011 - 09:06 AM, said:

i've done what you've told me but I can still access the pages without logging in...


I'd honestly suggest you use $_SESSION variables and cookies instead of session_register.


I wish I knew how...
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: PHP session not working correctly

Posted 23 March 2011 - 06:16 AM

see post #2 and here.
Was This Post Helpful? 0
  • +
  • -

#14 Addio569  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 316
  • Joined: 26-November 08

Re: PHP session not working correctly

Posted 23 March 2011 - 06:20 AM

View PostDormilich, on 23 March 2011 - 06:16 AM, said:

see post #2 and here.


so..

<?php
session_start();

if (!isset($_SESSION['$myusername'])) {
  $_SESSION['$myusername'] = 0;
} else {
  $_SESSION['$myusername']++;
}
?>


Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,236
  • Joined: 08-June 10

Re: PHP session not working correctly

Posted 23 March 2011 - 06:24 AM

like that.

PS. as the $ sign is typical for a variable, it is best to avoid it in (array) key names.
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »