Jump to content

Archived

This topic is now archived and is closed to further replies.

Ninjakreborn

larger code on one page, or spread out(bottom of post)

Recommended Posts

I have been trying to think of a way to create a function, where at the beginning of the pages, where I am doing compact programs, with a lot of stuff, where variables can be get, or post, or a combination.  I am trying to setup something to take all post, adn get variables and change them to regular variables.
If I do this is it going to work the way I need, with out any issues, I just thought of it.

If I go to the top of the page, that I need it done at, and put

<?php
session_start(); // I use sessions some so I have to have this
extract($_POST);
extract($_GET);
extract($_SESSION);
extract($_ENVIROMENT);
extract($_SERVER);
?>
this wasn't very realistic, I wouldn't have a need to do this all the time, or use all of these at once, but I was wondering, for instance, if I had a lot of post's and get's coming to the page, along with some sessions, and I wanted to make all of them regular variables.  Would this work

For instance if I have 2 sessions
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
for instance
and they are registered, then on the page, that has the active sessions, in comes 3 get variables
$_GET['hello'];
$_GET['what'];
$_GET['decide'];
Those 3 are coming in for instance, then if I put

<?php
session_start();
extract($_SESSION);
extract($_GET);
?>
Would I then be able to get to sessions and other variables directly, is this safe.
The last, and main question, if it's safe, why haven't I ever seen anyone else do this before?

Share this post


Link to post
Share on other sites
This is not safe. Basically what your doing is coding in register_gloabls without register_globals being on. You;re pretty much [i]simulating[/i] register_globals being on.

Also if you have a say a get and sessions variable with the same name. Then one or the other will overwrite the other.

Why do you want to us [i]normal[/i] variables rather than use the superglobals. If you use the superglobals you wont have to worry about your variables being overwritten and your app will be much safer.

Share this post


Link to post
Share on other sites
I normally do like that, but I am starting to try to build a larger cms, without as many pages.

Someone told me the more pages the less the abilities or something.
I wanted to try to use programming logic to do as much as I can on one page, using a status variable and other stuff
but when I have a lot of different get's coming it's hard to keep up with.

EDIT
What about this
if ($_GET['status']) {
$status = $_GET['status'];
}elseif ($_POST['status']) {
$status = $_POST['status'];
}

I have seen even other programmers do things like this, in a tightly packed program.
Is this going to be safe.

Share this post


Link to post
Share on other sites
To make it safe you first want to check that satus is not set in POST and GET at the same time.
[code=php:0]// check that status is only set in either GET or POST and not both!
if(isset($_GET['status']) && isset($_GET['staus']))
{
    die('Hacking attempt');
}
elseif(isset($_GET['status']))
{
    $status = $_GET['status'];
}
elseif(isset($_POST['status']))
{
    $status = $_POST['staus']
}
else
{
    $status = 'Defualt value';
}[/code]

Share this post


Link to post
Share on other sites
is
if (isset($_GET['status']) {
$status = $_GET['status'];
}elseif (isset($_POST['status']) {
$status = $_POST['status']) {
}

Is that the same as
if ($_GET['status']) {
$status = $_GET['status'];
}elseif ($_POST['status']) {
$status = $_POST['status'];
}
Are those 2 things, the same thing actually.
Meaning do they function the same, and I will keep in mind about what you said as far as checking for the hacking attempt.

Share this post


Link to post
Share on other sites
No they are not the same. The following:
[code=php:0]if ($_GET['status']) {
$status = $_GET['status'];
}elseif ($_POST['status']) {
$status = $_POST['status'];
}[/code]

Doesnt check for the [b]existance[/b] of the variable, but checks whether $_GET['status'] is either true (it will return true if it holds a boolean, string, numeric value etc) or false. Where as if(isset($_GET['status'])) checks whether the variable exists and not the value

You should use isset for check whether a variable exists, never if($_GET['status'])

Share this post


Link to post
Share on other sites
[quote author=businessman332211 link=topic=110953.msg449249#msg449249 date=1160402656]
Someone told me the more pages the less the abilities or something.[/quote]

That's complete rubbish.

Share this post


Link to post
Share on other sites
That is not good, because I have been doing it that way for a long time, on all my older projects, what problems can that pose later on down the road.

Also when it comes down to the thing he told me
so if I can have a program done, and I end up doing it in 17 pages, I redo it in 2 pages, it's no better than the one on 17 pages??

Share this post


Link to post
Share on other sites
please your telling us that all the code you see on here that you have not valadated you $_GET method that outrageus.

if you dont valadate a $_GET[''] within a comming link then your codes are all flawed for emaple a user can change and acces all users information buy adding the required infromation to the link.

//a quick example
[code]
link example
<?php

echo"<a href='members.php?id=$id&name=$name&password=$password&cmd=access'>go to members page</a>";

//The link has got a condition that is cmd = access then do the get otherwise pee off hacker. 

?>


members.php
<?php

if(isset($_GET['cmd']=="access")){

$id=$_GET['id'];
$name=$_GET['name'];
$password=$_GET['password'];

echo " hi there your in <br> $id <br> $name <br> $password<br>";

}else{

echo "go away hacker";

}

?>
[/code]

Share this post


Link to post
Share on other sites
[quote]Someone told me the more pages the less the abilities or something.

That's complete rubbish.[/quote]
That, I asked
[quote]Also when it comes down to the thing he told me
so if I can have a program done, and I end up doing it in 17 pages, I redo it in 2 pages, it's no better than the one on 17 pages??[/quote]
I am confused now, so it isn't true that the lower number of pages taken to do something the better.
Can someone explain this some, I was always trying to fit as much programming as humanly possible on one page, instead of multiple pages, what is a good medium, this is somethign I really wondering about now.

Share this post


Link to post
Share on other sites
The number of pages your code uses has nothing to do with how well or how quickly it runs.  The only reason someone may have told you that is if you tend to add in a lot of extra code that doesn't actually do anything.  For example, the two snippets here will run exactly the same, despite the extra whitespace and comments, and one of them is a lot more readable:

[code]if ($_GET['status']) {
$status = $_GET['status'];
}elseif ($_POST['status']) {
$status = $_POST['status'];
}[/code]

[code]//Something about what this if statement is for

if ($_GET['status']) {
    $status = $_GET['status'];
}

elseif ($_POST['status']) {
    $status = $_POST['status'];
}[/code]

Your 2 page version probably does run faster than your 17 page version, because the only way you could get rid of those 15 extra pages is by taking out a whole lot of stuff that the computer didn't need to be doing.  But it's not the number of pages that made the difference, it's the number of calculations and the amount of memory needed to do them.  I don't know how to optimize php at all, cuz I don't do anything that really needs it, but I'm sure you could find stuff on Google if you really wanted to know how to make your scripts either run faster or use less memory.

Share this post


Link to post
Share on other sites
It doesnt matter how many files you use. You can code a whole app in just one file if you want. However this file will be huge and have tons of lines of code. Hard to debug, edit etc.

Having multiple files is much easier to handle. As you can easily find what you wnat to edit. Rather than opening up your big file and go where is that function or whatever.

Share this post


Link to post
Share on other sites

×

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.