Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php function available on other php page

Tags:

php

session

I have a function that I use on index.php page and I would like to call it from other php page (other.php). How to make this function available without redeclaration? I think it's achievable using sessions, but I am not sure how to do it exactly.

Th problem is that it works in index.php, because it uses some API declaration, but it doesn't on other.php. I am not sure how to setup the API on other.php page, so I will need some sort of session pass rather than seperate file with functions. Any ideas?

EDIT: Maybe it's confusing, so I will try to clarify it. I have a page let's say index.php with a function: get_loggedin_user(); . It prints the user's name. It works in index.php because it is a part of a CMS system with an API and uses this API. The problem is that I would like to use this function on (or in worst case pass the user's name) to other page (other.php) which is accessible by the link form index.php. Now I would like to print user's name on other.php. Is this achievable? I know I can pass the name using sessions and I would like to know how to do this, or if it is possible, how to access this function. Hope it's clear now.

like image 734
Vonder Avatar asked Nov 24 '25 14:11

Vonder


1 Answers

You'ld be better of having your functions declared in one php file and include it in all php files where you need it.

<?php

// require_once will prevent a file being included multiple times
// and so prevents functions from being redeclared again (which would cause errors)
require_once 'yourfilewithfunctions.php';

$result = call_your_function( $with, $parameters );

?>
like image 94
Decent Dabbler Avatar answered Nov 26 '25 02:11

Decent Dabbler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!