FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


$_POST on Includes





DanielXP
Hello,

I have a main page.

index.php

What includes

include("template/defualt/index.php");

And what i want is when a form is posted.

the $_POST["username"] wont work inside the template/defualt/index.php file.

I can't use a index.php?username=$_POST["username"] in the url to do it that way because its going to a few hundred posts and gets all over the site.

My friend said i can do it with globals but im not sure how to get them to work.

Thanks if you can help me!
AftershockVibe
I can't really work out what you're trying to do from your post but I think what you should be looking at for things like "saving" username between pages is $_SESSION varaibles.

It will require you to have session_start() at the beginning of every page and this must come before any other output. Having a look here should get you started:
http://www.w3schools.com/php/php_sessions.asp
mathiaus
the $_POST var is accessible from included files :\
DanielXP
AftershockVibe wrote:
I can't really work out what you're trying to do from your post but I think what you should be looking at for things like "saving" username between pages is $_SESSION varaibles.

It will require you to have session_start() at the beginning of every page and this must come before any other output. Having a look here should get you started:
http://www.w3schools.com/php/php_sessions.asp


I was thinking about using a session.

But would you be able to do like a foreach of the $_POSTs?

mathiaus wrote:
the $_POST var is accessible from included files :\


How?
DanielXP
I have found a solution for my problem.

Code:
foreach($_POST as $key => $value) {
$addon_url .= "&" . $key . "=" . $value;
}

include("template/defualt/index.php?something=s" . $addon_url);


Seems to work for me ok.
snowboardalliance
DanielXP wrote:
I have found a solution for my problem.

Code:
foreach($_POST as $key => $value) {
$addon_url .= "&" . $key . "=" . $value;
}

include("template/defualt/index.php?something=s" . $addon_url);


Seems to work for me ok.


You should use sessions if you have a large amount of $POST variables. If you keep them in the URL, weird things can happen, like a user sends a link to someone and it has all of their data. Sessions aren't perfectly secure, but they are a lot better.
DanielXP
snowboardalliance wrote:
DanielXP wrote:
I have found a solution for my problem.

Code:
foreach($_POST as $key => $value) {
$addon_url .= "&" . $key . "=" . $value;
}

include("template/defualt/index.php?something=s" . $addon_url);


Seems to work for me ok.


You should use sessions if you have a large amount of $POST variables. If you keep them in the URL, weird things can happen, like a user sends a link to someone and it has all of their data. Sessions aren't perfectly secure, but they are a lot better.


Code:
<?php
ob_start(); session_start();
echo $_SESSION['username'];

foreach($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
echo $_SESSION['username'];
include($site[url] . "template/" . $template . "/index.php?url=" . $site[url] . "&page=" . $page . "&page2=" . $page2);
echo $_SESSION['username'];
?>


Thats my new code.

The first echo session echos the last one (as it should)
The second echos the session now (as it should)
But the third one don't echo any thing
And on the included page don't seem be able to get the sessions.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.