Jump to content

cant get sessions to work


scottreid1974

Recommended Posts

Hi,

i have a logon page where users enter a password, and user name, and they then get redirected to another web page.  What i want is to make all the pages they visit when logged on secure, presumably using sessions. I am not sure of the code to use to create the sessions on the logon page, and the validation type sessions on the following visited pages.


The logon page code is below.


Thanks



<?php

if (!isset($_SESSION)) {
  session_start();
}


$loginUsername = $_POST['username'];
$password = $_POST['password'];

mysql_select_db($database_testconn1, $testconn1);

$sql = "SELECT username, password, url, access FROM users WHERE username='$loginUsername' AND password='$password' LIMIT 1";
$user = mysql_fetch_assoc(mysql_query($sql, $testconn1));

if(count($user) > 0){
    header('Location: '.$user['url']);
}else{
    header('Location: login.php');
}
?>
Link to comment
Share on other sites

Tried the tutorials, but i still cant get it to work,


The problem is, i can get sessons to work on one webpage, but i cant when trying to get it to work on a webpage retrieved from a database, when user logs in . The url is stored in  a field in the database, and is specific to each log on (account)


any help plllzzz



Link to comment
Share on other sites

Hi Scott,

Each subsequent page that uses your session or session data will have to have a [code]session_start() [/code] at the very top of the page, before any other output is sent.

For example:

[code]
<?php
session_start()
?>
<html>
<head><title>Any page on your site</title></head>
<body>
The page data.
</body>
</html>
[/code]

Hope that helps you.
Link to comment
Share on other sites

Scott,

Your problem is that you aren't setting the session variables anywhere.  As soon as your user is confirmed as being logged in, you're redirecting them to their homepage.

This code:
[code]<?php
if(count($user) > 0){
  header('Location: '.$user['url']);
}
?>[/code]

Should look more like this:
[code]<?php
if(count($user) > 0){
  $_SESSION['username'] = $user['username']; // This assigns the value to a session variable
  header('Location: '.$user['url']);
}
?>[/code]

Check your email ;)

Regards
Huggie
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.