Forum Moderators: coopster

Message Too Old, No Replies

Using includes()

         

Jamier101

2:19 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



At the minute I'm having to retype code or at least copy and past it because I don't seem to be able to using include files, has anyone else struggled with this as it seems pretty straight forward.


include("includes/security.php");


inside the include file:

security.php

<?php
if (! isset($_SESSION['id'])){

header("location: login.php");
exit;
}
?>


I can make the file work perfectly if I don't use the include but I'd really like to make it work.

enigma1

2:49 pm on Oct 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



replace the include with require see what the error says.

require('includes/security.php');

or enable the warnings put at the top of the file:

error_reporting(E_ALL|E_STRICT);

Jamier101

3:04 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



Nothing, it simply redirects me back to the login page like its told to if there's an issue.

login.php


<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bigboldy"; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

session_start();

$userid = $_POST['userid'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userid && $password){
/////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM users WHERE username='$userid' and password='$password'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////
}

if ($rowAccount){

$_SESSION['id'] = $rowAccount['username'];

header("location:welcome.php");
exit;

}elseif($submitted){

echo "You dont exist in the system so your not getting in";
}

?>


welcome.php

<?php

error_reporting(E_ALL|E_STRICT);

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bigboldy"; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

require("includes/security.php");

session_start();

$id = $_SESSION['id'];

/////////////////////////////////////////////////////////////////////////
$query = sprintf("SELECT * FROM users WHERE username='$id'");
$result = @mysql_query($query);
$rowAccount = @mysql_fetch_array($result);
/////////////////////////////////////////////////////////////////////////

?>

henry0

5:41 pm on Oct 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to get in the habit of setting your session_start at the script top.
look at your welcome you are including before starting the session so even if the call is legit the session you expect cannot be found.

Jamier101

10:51 pm on Oct 2, 2010 (gmt 0)

10+ Year Member



Thank you, its now working a treat :)