Jump to content

Sessions..


rofl90

Recommended Posts

Are you asking how to get the value of the session?

 

<?php
session_start();

//print out the username stored in the session
echo $_SESSION['username'];

//Assign the session to a variable
$var = $_SESSION['username'];

?>

 

As for storing it in the database, you would just use an INSERT query. This site has a lot of tutorials that would help you: http://www.tizag.com/phpT/

Link to comment
Share on other sites

ok thanks, btw it didn't work the echo session user thingy..

heres my validation

 

Validating your credentials... Please wait a moment...

<?php
/* get the incoming ID and password hash */
$user = $_POST["userid"];
$pass = md5($_POST["password"]);

/* establish a connection with the database */
$server = mysql_connect("x", "x",
          "x");
if (!$server) die(mysql_error());
mysql_select_db("x");
  
/* SQL statement to query the database */
$query = "SELECT * FROM users WHERE User = '$user'
         AND Password = '$pass'";

/* query the database */
$result = mysql_query($query);

if (mysql_fetch_row($result)) {
  /* access granted */
  
  $sql = "update users set online='<img src='http://www.codeetech.com/backend/images/online.png' />' WHERE User = '$user'";
  mysql_query($sql);
  
  session_start();
  header("Cache-control: private");
  $_SESSION["access"] = "granted";
  header("Location: index2.php");
  
} else
  /* access denied &#8211; redirect back to login */
  header("Location: index.php");
?>

Link to comment
Share on other sites

<?php
session_start()

$dbhost = 'x';
$dbuser = 'x';
$dbpass = 'x';
$dbname = 'x';
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());

mysql_select_db($dbname);
$sql = "update users set online='<img src='xxxxxxxxx' />'";
mysql_query($sql);

      session_destroy();	  
      header("Location: index.php");
  
?>

 

and theres logout, that doesn't work either, just keeps me logged in...

Link to comment
Share on other sites

Your last post was 12 minutes before your bump... if it's really that urgent, why not learn the language?

 

To answer your question, when you call session_destroy, the session is still considered active for that execution. Try redirecting (to the same page, if you'd like) using the header() function after your session_destroy call

Link to comment
Share on other sites

I am heres the code

 

<?php
session_start()

$dbhost = 'x';
$dbuser = 'x';
$dbpass = 'x';
$dbname = 'x';
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());

mysql_select_db($dbname);
$sql = "update users set online='<img src='xxxxxxxxx' />'";
mysql_query($sql);

      session_destroy();	  
      header("Location: index.php");
  
?>

Link to comment
Share on other sites

in this example:

<?php
/* get the incoming ID and password hash */
$user = $_POST["userid"];
$pass = md5($_POST["password"]);

/* establish a connection with the database */
$server = mysql_connect("x", "x",
          "x");
if (!$server) die(mysql_error());
mysql_select_db("x");
  
/* SQL statement to query the database */
$query = "SELECT * FROM users WHERE User = '$user'
         AND Password = '$pass'";

/* query the database */
$result = mysql_query($query);

if (mysql_fetch_row($result)) {
  /* access granted */
  
  $sql = "update users set online='<img src='http://www.codeetech.com/backend/images/online.png' />' WHERE User = '$user'";
  mysql_query($sql);
  
  session_start();
  header("Cache-control: private");
  $_SESSION["access"] = "granted";
  header("Location: index2.php");
  
} else
  /* access denied &#38;#8211; redirect back to login */
  header("Location: index.php");
?>

 

you don't session_start() until the bottom of the code. I was told session_start() was always supposed to be at the very beginning of the script.

Link to comment
Share on other sites

There is no session data in that post to even destroy.

 

I am heres the code

 

<?php
session_start()

$dbhost = 'x';
$dbuser = 'x';
$dbpass = 'x';
$dbname = 'x';
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());

mysql_select_db($dbname);
$sql = "update users set online='<img src='xxxxxxxxx' />'";
mysql_query($sql);

      session_destroy();	  
      header("Location: index.php");
  
?>

Link to comment
Share on other sites

I'm asking about destroying a session: heres login.php

 

 

<?php
error_reporting(E_ALL);
$dbhost = 'x';
$dbuser = 'x';
$dbpass = 'x';
$dbname = 'x';
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());

mysql_select_db($dbname);
?>
<form action="validate.php" method="post"> <fieldset>
<label>Your IP is <?php echo $_SERVER['REMOTE_ADDR']; ?>, and has been logged, unauthorised attempts to access will be logged, and steps will be taken. <br /></label>
<br />
<label>
<input type="text" name="userid" id="user" />
Name</label>
<br /><br />
<label> 
<input type="password" name="password" id="password" />
Password</label>
<br />
<br />
<input type="submit" id="submit" value="Login" />
<input name="ip" type="hidden" id="hidden" value="<?php $ip = $_SERVER['REMOTE_ADDR']; echo $ip; ?>" />
<br />
<br />
Backend time out currently set at: <?php $result = mysql_query("SELECT * FROM settings") or die(mysql_error()); 
$row = mysql_fetch_array($result);
$timing = $row['timeout']; echo $timing / 60; ?> minutes.<br />
</fieldset></form>

 

and validate.php:

 

Validating your credentials... Please wait a moment... 
<?php
/* get the incoming ID and password hash */
$user = $_POST["userid"];
$pass = md5($_POST["password"]);

/* establish a connection with the database */
$server = mysql_connect("mx", "x",
          "hxx");
if (!$server) die(mysql_error());
mysql_select_db("xx");
  
/* SQL statement to query the database */
$query = "SELECT * FROM users WHERE User = '$user'
         AND Password = '$pass'";

/* query the database */
$result = mysql_query($query);

if (mysql_fetch_row($result)) {
  /* access granted */
  
  $sql = "update users set online='<img src='xx.png' />' WHERE User = '$user'";
  mysql_query($sql);
  
  session_start();
  header("Cache-control: private");
  $_SESSION["access"] = "granted";
  $_SESSION["user"] = $user;
  header("Location: index2.php");
  
  
} else
  /* access denied &#8211; redirect back to login */
  header("Location: index.php");
?>

Link to comment
Share on other sites

As stated before, you need to put session_start() as the first line of code.

 

login.php doesn't even have session data in it.

 

validate.php has the session_start() in the middle

 

Again, where are you seeing yourself logged in after you logout?

Link to comment
Share on other sites

But I don't want to start sessions in validate.php unless the information is correct...

 

and i get a white screen when i clickk logout.php also if this is any help the vars are

 

Array ( [access] => granted [user] => Charlie ) 1

Link to comment
Share on other sites

this is my entire logout.php script

 

<?php 

session_start();  
$id = $_SESSION['id'];

connect to db.

mysql_query("UPDATE `users` SET `online_now` = 'no' WHERE `id`='{$id}' ") or die(mysql_error());

session_destroy();

echo "<meta http-equiv='refresh' content='0;url=http://localhost/index.php?source=logged_out'>";
?>

now obviously its extremely simple, but its works.

Link to comment
Share on other sites

I get a blank page with:

 

In PHPDES08 its saying dbhost is an unexpected t-var?????????

 

<?php
session_start()

$dbhost = 'x';
$dbuser = 'x';
$dbpass = 'x';
$dbname = 'x';
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());

mysql_select_db($dbname);

$user = $_SESSION['user'];

mysql_query("UPDATE `users` SET `online` = 'offline' WHERE `user`='{$user}' ") or die(mysql_error());

session_destroy();

echo "<meta http-equiv='refresh' content='0;url=http://codeetech.com/backend/index.php'>";
?>

Link to comment
Share on other sites

updatethe mysql error is:

 

Validating your credentials... Please wait a moment... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'http://www.codeetech.com/backend/images/online.png' border='0' />' WHERE user = ' at line 1

 

and the codeis:

Validating your credentials... Please wait a moment... 
<?php
/* get the incoming ID and password hash */
$user = $_POST["userid"];
$pass = md5($_POST["password"]);

/* establish a connection with the database */
$server = mysql_connect("x", "px",
          "x");
if (!$server) die(mysql_error());
mysql_select_db("xx");
  
/* SQL statement to query the database */
$query = "SELECT * FROM users WHERE User = '$user'
         AND Password = '$pass'";

/* query the database */
$result = mysql_query($query);

if (mysql_fetch_row($result)) {
  /* access granted */
  
  $sql = "update users set online='<img src='http://www.codeetech.com/backend/images/online.png' border='0' />' WHERE user = $user";
  mysql_query($sql) or die(mysql_error());
  
  session_start();
  header("Cache-control: private");
  $_SESSION["access"] = "granted";
  $_SESSION["user"] = $user;
  header("Location: index2.php");
  
  
} else
  /* access denied &#8211; redirect back to login */
  header("Location: index.php?a=Login has failed, please try again.");
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.