...

View Full Version : creating a function



harlequin2k5
07-13-2007, 08:32 PM
a new site I'm working on has a login function and a user type check on the pages within - I'm attempting to create a bounce function because the site is currently on my server but I know at some point it will get moved from there and if you think I want to go through page by page to change the redirect url it's not going to happen

anywho - this is how it currently looks on most of the pages:


if ($_SESSION['users'] == "1")

{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();

}

I've attempted with:


function bounce_in()
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();
}

which I then tried to use as:


if ($_SESSION['users'] == "1")

{
bounce_in();

}

bounce_in requires you to log in twice (you can login though) and I also made a bounce_out but that generates a function not defined error

any help is greatly appreciated :)

_Aerospace_Eng_
07-13-2007, 08:39 PM
So whats the issue? To fix the function not defined error make sure the function is in the server side code before its called. Not sure what to tell you about the logging in twice thing as you've given us very little code. Why are you using session_write_close() anyways? I suspect that might be your problem with having to log in twice. It locks the session data or only the script using it can write to it.

harlequin2k5
07-13-2007, 08:46 PM
the main issue is that I wanted to have a simple way of updating the header url when this site moves from my server

I'm reusing code from a previous attempt at the same type of site (a simple intranet) and I've never made my own function before

the complete php code from the index.php is:


<?
session_start();
include_once("functionlist.php");

if ($_SESSION['users'] == "1")

{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();
}

else if ($_SESSION['users'] == "2")
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();

}

else if ($_SESSION['users'] == "3")
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();

}

$error_message = ""; // Set error message to empty string

// Check to see if form has been submitted
if (isset($_POST['submit']))
{

// Grab username and password
$userid = $_POST['username'];
$userpassword = $_POST['password'];

// Make connection to DB and query database for username, password, and group
$conn = doconnect();

$query = "SELECT username, password, typeid FROM users WHERE username = '" . safe($userid, $conn) . "' AND password = '" . safe($userpassword, $conn) . "' AND active= 'y'";
;
$result = mysql_query($query, $conn) or die('Query Error - ' . mysql_error());
$numRows = mysql_num_rows($result);
if($numRows > 0)
{
$row = mysql_fetch_assoc($result);

$realID = $row['username'];
$realPass = $row['password'];
$users = $row['typeid'];

// Find group and redirect them after setting their session
$_SESSION['users'] = $users;
$_SESSION['userid'] = $userid;

if ($_SESSION['users'] == "1")

{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();

}

else if ($_SESSION['users'] == "2")
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();

}

else if ($_SESSION['users'] == "3")
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();

}

else
{
$error_message = "<p style=\"font-weight:bold;color:#ff0000;font-size:14px;\">Could not find you in our system...<br />\nPlease try again...</p>\n";
}

}
else
{
$error_message = "<p style=\"font-weight:bold;color:#ff0000;font-size:14px;\">Incorrect username/password!<br />\nPlease try again...</p>\n";
}
}
?>

the two bounce functions are:


function bounce_in()
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/main-index.php");
exit();
}

function bounce_out()
{
session_write_close();
header("Location: http://shiznit.no-ip.info/lane/index.php");
exit();
}

Fumigator
07-13-2007, 10:24 PM
Not sure how the PHP pros do it but I use a file which I call env.php which stores a bunch of constants such as path, MySQL connection info, etc. etc... these are values that will be different depending on where the script is run.

I then store different versions of that file in the code library, and then simply use the version of the file appropriate to the environment I am running in.

In addition, you can use $_SERVER variables to derive the domain name to use with the header() function so that it redirects to the current directory regardless of the domain it is running on. Here's what I use:



page_redirect('home.php');

function page_redirect ($destinationPage) {
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header("Location: http://$host$uri/$destinationPage");
}

StupidRalph
07-14-2007, 05:04 AM
@ FUMIGATOR
Do you mind posting that in my thread?
http://www.codingforums.com/showthread.php?t=118611

@Harlequin2k5
I really don't understand the session_write_close() unless you're currently redirecting to a different server and you're ensuring that the session is closed on yours.

harlequin2k5
07-14-2007, 06:55 AM
I don't even remember where I got the session_write_close() from - if there's a better way to create a login/logout then by all means enlighten me

I've been reading through my "PHP and MySQL Web Development" book and unfortunately the user system they have is all tied together with an online store application and I'm having too much difficulty pulling it back apart

I don't need anyone to build it for me - even a google keyword would work or what to look for on hotscripts or something

Fumigator
07-14-2007, 07:00 AM
I just copied the example code from the manual for my session destroying needs... seems to do the job.

http://us.php.net/manual/en/function.session-destroy.php

harlequin2k5
07-16-2007, 05:26 PM
let me try this again...

it doesn't matter to me whether it's a session_ or login function that I use - I only used the session_write because that's all I could get my hands on that I knew worked

I've searched high and low for a login function that would work and have been unsuccessful in finding one for what I need

the session is not the issue - like I said I know this is going to work for what I need - the issue is that I need a way to be able to store the header location url
header("Location: http://shiznit.no-ip.info/lane/main-index.php"); in such a way that I can go back and change the location url once this site is moved off of my server

I've tried to use includes and tried to create my own function and neither have worked

Fumigator
07-16-2007, 06:22 PM
The code I posted in post #4 of this thread won't work because...?

harlequin2k5
07-16-2007, 09:00 PM
The code I posted in post #4 of this thread won't work because...?

fumi - I'm sorry - that worked just fine :) brando had to help me with it a bit more cuz I'm dense but that was exactly what I needed



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum